国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

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

2019-11-09 14:13:19
字體:
來源:轉載
供稿:網友

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

1)線性布局:利用屬性android:weightSum和android:layout_weight來實現

2)線性布局:利用屬性android:layout_weight和隱藏無關控件的方式來實現

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

下面分別給出測試代碼:

1.利用android:weightSum和android:layout_weight來實現

布局文件如下:

<?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和隱藏無關控件的方式來實現布局文件如下:

<?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.通過布局文件和代碼動態修改寬度屬性

布局文件如下:

<?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 * 通過代碼動態修改控件的寬度占屏幕的一半 */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);        //獲取布局參數        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;    }}以上三種方式出現的效果是一樣的,由于布局比較簡單,這里就不貼圖了,代碼可以直接運行,大家可以直接看到效果


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 斗六市| 垫江县| 重庆市| 军事| 喀什市| 沙雅县| 南郑县| 布尔津县| 甘德县| 宁波市| 蓝山县| 和田县| 大连市| 图们市| 和平县| 玉溪市| 衡水市| 吉林省| 益阳市| 南康市| 黑龙江省| 平塘县| 晋江市| 库伦旗| 图们市| 仁布县| 昌黎县| 商丘市| 陕西省| 玛多县| 霍林郭勒市| 乌拉特前旗| 凤阳县| 城步| 清苑县| 谢通门县| 浦北县| 昔阳县| 岑溪市| 台南市| 南和县|