簡體   English   中英

Android中的布局元素控件

[英]layout element control in Android

我想像這樣在欄上布局3個元素Button,TextView,Button

[Button1] --- TextView --- [Button2]

2個按鈕始終固定在左右屏幕上(填充4dp),TextView居中(更改寬度取決於屏幕大小),它們應適用於所有屏幕大小(不在大屏幕上縮放)。 我該怎么做??

看起來像這樣。-

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" />

</RelativeLayout>

請注意,您仍然需要設置其他屬性,例如文本,...

您可以將LinearLayout與android:weightSum一起使用

像這樣嘗試,通過使用android:weightSum ,您可以為Button和textView划分多少空間。並且它將在所有密度設備中自動調整。

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="10" >

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_margin="4dp"
        android:layout_weight="3"
        android:text="LeftButton" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:padding="4dp"
        android:layout_weight="4"
        android:gravity="center"
        android:text="Center TextView text" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_margin="4dp"
        android:layout_weight="3"
        android:text="Right Button" />

</LinearLayout>

android:weightSum對我來說工作正常,我解決了所有麻煩:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM