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

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

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

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

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

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;    }}以上三種方式出現的效果是一樣的,由于布局比較簡單,這里就不貼圖了,代碼可以直接運行,大家可以直接看到效果


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永城市| 陇西县| 恩施市| 曲阜市| 射洪县| 津南区| 读书| 娱乐| 宾阳县| 塘沽区| 芒康县| 沙湾县| 白朗县| 邵东县| 多伦县| 大宁县| 汝州市| 城市| 崇左市| 依兰县| 安图县| 荣成市| 新源县| 神池县| 九龙县| 会理县| 南投县| 静乐县| 庆云县| 景洪市| 博白县| 晋江市| 合江县| 皮山县| 于都县| 武义县| 潮安县| 惠水县| 社旗县| 淳化县| 乡宁县|