麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

安卓-控制控件的寬度占屏幕的一半且水平居中顯示

2019-11-09 14:13:19
字體:
供稿:網(wǎng)友

今天說下如何讓一個控件的寬度顯示時占屏幕的一般寬度,且水平居中顯示。這里拋磚引玉,給出三種實(shí)現(xiàn)方案:

1)線性布局:利用屬性android:weightSum和android:layout_weight來實(shí)現(xiàn)

2)線性布局:利用屬性android:layout_weight和隱藏?zé)o關(guān)控件的方式來實(shí)現(xiàn)

3)線性布局:通過布局文件和代碼動態(tài)修改控件的布局中地方寬度參數(shù)屬性

下面分別給出測試代碼:

1.利用android:weightSum和android:layout_weight來實(shí)現(xiàn)

布局文件如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center|top"    android:background="#FFFFFF"    android:weightSum="1">    <Button        style="?android:attr/buttonBarStyle"        android:id="@+id/idd_btn_one"        android:layout_marginTop="10dp"        android:background="#FF0000"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:text="寬度占屏幕的一半"        android:layout_weight="0.5"/></LinearLayout>2.利用屬性android:layout_weight和隱藏?zé)o關(guān)控件的方式來實(shí)現(xiàn)布局文件如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:layout_width="match_parent"              android:layout_height="match_parent"              android:gravity="top"              android:orientation="horizontal"              android:background="#FFFFFF">    <Button        style="?android:attr/buttonBarStyle"        android:id="@+id/idd_btn_one"        android:layout_marginTop="10dp"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:text="不顯示1"        android:visibility="invisible"        android:layout_weight="1.03"/>    <Button        style="?android:attr/buttonBarStyle"        android:id="@+id/idd_btn_two"        android:layout_marginTop="10dp"        android:background="#00FF00"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:text="寬度占屏幕的一半"        android:layout_weight="2"/>    <Button        style="?android:attr/buttonBarStyle"        android:id="@+id/idd_btn_three"        android:layout_marginTop="10dp"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:text="不顯示2"        android:visibility="invisible"        android:layout_weight="1"/></LinearLayout>3.通過布局文件和代碼動態(tài)修改寬度屬性

布局文件如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/activity_half_of_screen_three"    android:orientation="horizontal"    android:gravity="center|top"    android:layout_width="match_parent"    android:layout_height="match_parent">    <Button        style="?android:attr/buttonBarStyle"        android:id="@+id/id_btn_one"        android:layout_marginTop="10dp"        android:background="#0000FF"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:gravity="center"        android:text="寬度占屏幕一半"        /></LinearLayout>代碼如下:

import android.graphics.Point;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.ViewGroup;import android.widget.Button;import com.mobile.cdtx.blog.R;/** * created by wangwentao 2017/2/10 * 通過代碼動態(tài)修改控件的寬度占屏幕的一半 */public class HalfOfScreenActivityThree extends AppCompatActivity {    @Override    PRotected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_half_of_screen_three);        Button btn = (Button) findViewById(R.id.id_btn_one);        //獲取布局參數(shù)        ViewGroup.LayoutParams lp = btn.getLayoutParams();        lp.width = getScreenWidth()/2;        btn.setLayoutParams(lp);    }    //獲取屏幕的寬度    public int getScreenWidth() {        Point point = new Point();        getWindowManager().getDefaultDisplay().getSize(point);        return point.x;    }    //獲取屏幕的高度    public int getScreenHeight() {        Point point = new Point();        getWindowManager().getDefaultDisplay().getSize(point);        return point.y;    }}以上三種方式出現(xiàn)的效果是一樣的,由于布局比較簡單,這里就不貼圖了,代碼可以直接運(yùn)行,大家可以直接看到效果


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 高清国产午夜精品久久久久久 | 欧美一级电影网 | 久久久久久久久久亚洲 | 日韩精品a在线观看 | 免费在线观看国产精品 | av不卡免费在线 | 日韩视频在线观看免费视频 | 看一级毛片 | 国产自在自线午夜精品视频在 | 爱逼爱操综合网 | 久久久久久久久久久av | 色综合久久99 | 国产精品亚洲三区 | 国产成年人在线观看 | av在线免费看片 | 久久生活片 | 中文字幕偷拍 | 国产中出视频 | 国产一区免费观看 | fc2成人免费人成在线观看播放 | 欧美1区2区在线观看 | 美女喷水网站 | 国产午夜亚洲精品 | 国产一级午夜 | h视频免费观看 | 精品人成 | 成人免费自拍视频 | 成码无人av片在线观看网站 | 国产免费久久久久 | 99国产精品自拍 | 欧美亚洲黄色 | 久久在线免费视频 | 国产无限资源在线观看 | 草草视频在线 | 国产精品美女久久久久久网站 | 成人不卡 | 国产成人精品免费视频大全办公室 | 久久经典免费视频 | a一级黄色大片 | 在线免费观看毛片视频 | h视频免费看 |