简体   繁体   English

Android中的布局元素控件

[英]layout element control in Android

I want to layout 3 element Button , TextView , Button on the bar like this 我想像这样在栏上布局3个元素Button,TextView,Button

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

2 button are always anchor fixed in left and right screen(padding 4dp) ,and TextView is center (change width depend on screen size),They should be apply for all screen size(not scaled in larger screen). 2个按钮始终固定在左右屏幕上(填充4dp),TextView居中(更改宽度取决于屏幕大小),它们应适用于所有屏幕大小(不在大屏幕上缩放)。 How can I do it?? 我该怎么做??

It would look something like this.- 看起来像这样。-

<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>

Notice that you still need to set other properties such as texts, ... 请注意,您仍然需要设置其他属性,例如文本,...

You can use LinearLayout with android:weightSum 您可以将LinearLayout与android:weightSum一起使用

Try Like this, By using android:weightSum , you can divide how much space for Button and textView.. And it will automatically adjust in all the density devices. 像这样尝试,通过使用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