三个按钮行 - >调整大小以适应父宽度 [英] Three buttons in line -> resize to fit parent width
问题描述
我有这样的布局
- 线性布局
- 滚动查看
- 在相对布局
- 9X按钮
- Linear Layout
- Scroll View
- Relative Layout
- 9x Button
在像这样的(3×3格)
In View like this (3x3 Grid)
+---------+ | o o o | | o o o | | o o o | +---------+
每个按钮都有它的背景,也没有文字,背景是这样的:
Every button has it's background and no text, background like this:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/menu_btn1_hover" android:state_pressed="true"/> <item android:drawable="@drawable/menu_btn1"/> </selector>
我应该
如何陈述的布局,所以按钮将永远每行3,并且将调整,使它们适合的看法?
How should I state the layout, so the buttons will be always 3 per line, and will resize so that they will fit the view ?
推荐答案
试试这个!
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/LinearLayout02" android:layout_height="wrap_content" android:layout_width="match_parent"> <Button android:id="@+id/Button04" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"></Button> <Button android:id="@+id/Button05" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"></Button> <Button android:id="@+id/Button06" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"></Button> </LinearLayout> <LinearLayout android:id="@+id/LinearLayout01" android:layout_height="wrap_content" android:layout_width="match_parent"> <Button android:id="@+id/Button01" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"></Button> <Button android:id="@+id/Button02" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"></Button> <Button android:id="@+id/Button03" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"></Button> </LinearLayout> <LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:layout_width="match_parent"> <Button android:id="@+id/button1" android:text="Button" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="0dp"></Button> <Button android:id="@+id/button2" android:text="Button" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="0dp"></Button> <Button android:id="@+id/button3" android:text="Button" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="0dp"></Button> </LinearLayout> </LinearLayout>
截图:
更新:
如果你做的LinearLayout(机器人:ID =@ + ID /包装(LOOK code以下))具有相同的layout_height和layout_width,你会得到你想要的。
If you make LinearLayout (android:id="@+id/wrapper" (LOOK CODE BELOW)) with same layout_height and layout_width you will get what you want
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/wrapper" android:layout_width="300dp" android:weightSum="1" android:orientation="vertical" android:layout_height="300dp"> <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="match_parent" android:layout_weight="0.33" android:layout_height="0dp"> <Button android:id="@+id/Button04" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"></Button> <Button android:id="@+id/Button05" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"></Button> <Button android:id="@+id/Button06" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"></Button> </LinearLayout> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="match_parent" android:layout_weight="0.33" android:layout_height="0dp"> <Button android:id="@+id/Button01" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"></Button> <Button android:id="@+id/Button02" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"></Button> <Button android:id="@+id/Button03" android:text="Button" android:layout_width="0dp" android:layout_weight="1" android:layout_height="fill_parent"></Button> </LinearLayout> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_weight="0.33" android:layout_height="0dp"> <Button android:id="@+id/button1" android:text="Button" android:layout_weight="1" android:layout_width="0dp" android:layout_height="fill_parent"></Button> <Button android:id="@+id/button2" android:text="Button" android:layout_weight="1" android:layout_width="0dp" android:layout_height="fill_parent"></Button> <Button android:id="@+id/button3" android:text="Button" android:layout_weight="1" android:layout_width="0dp" android:layout_height="fill_parent"></Button> </LinearLayout> </LinearLayout> </LinearLayout>
请看看截图。在第二个屏幕中的LinearLayout与Android:ID =@ + ID /包装具有相同的宽度和高度,等于300dp
Please look at screenshots. In second screenshot the LinearLayout with android:id="@+id/wrapper" have same width and height and equals 300dp
试试吧,请!希望它可以帮助你!
Try it please! Hope it help you!!
这篇关于三个按钮行 - &GT;调整大小以适应父宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Relative Layout
- Scroll View
- 在相对布局
- 滚动查看