Android: How to change divide the screen in 3 equal buttons

孤街醉人 提交于 2019-12-25 03:49:08

问题


I have 3 buttons in a layout. What I want to do is increase the size of the buttons so they fill the entire screen. How can I do that in the xml file?


回答1:


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

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_weight="1"/>

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button2"
    android:layout_weight="1"/>

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button3"
    android:layout_weight="1"/>
</LinearLayout>

You need weightSum




回答2:


You can use the weight property of a LinearLayout. I'm gonna make a guess and assume you want the buttons in a vertical layout.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button_1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/button_2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/button_3"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

</LinearLayout>

The Buttons will always fill the screen and you can play with the weight value to modify their height




回答3:


You need to put the buttons inside a LinearLayout and use weight:

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" 
android:weightSum="3">
<Button 
    android:layout_height="0dip"
    android:layout_width="wrap_content"
    android:layout_weight="1"
    />
<Button 
    android:layout_height="0dip"
    android:layout_width="wrap_content"
    android:layout_weight="1"
    />
<Button 
     android:layout_height="0dip"
    android:layout_width="wrap_content"
    android:layout_weight="1"
    />


来源:https://stackoverflow.com/questions/26242554/android-how-to-change-divide-the-screen-in-3-equal-buttons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!