我如何获得六个按钮根据屏幕大小来调整? [英] How do I get six buttons to resize based on the screen size?
问题描述
我创建了自己的汽车停靠的应用程序,我有6个按钮,让我把快捷方式的应用程序。我设计了横向和纵向模式的布局。我在使用屏幕大小在纵向模式下的问题。我有我的图标一字排开,像这样:
I'm creating a car dock application for myself and I have 6 buttons to allow me to place shortcuts to applications. I've designed a layout for landscape and portrait mode. I'm having issues with screen sizes in portrait mode. I have my icons lined up like so:
Text up here
------------
| 1 | 2 |
------------
| 3 | 4 |
------------
| 5 | 6 |
------------
下面是在XML的按钮:
Here's the XML for the buttons:
<Button
android:id="@+id/btnLaunchSlotOne"
style="@style/launchButton"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_weight="1"
android:layout_marginRight="8dp"
android:padding="10dp"
android:text="Navigation"
android:textColor="#ffffff" />
现在我遇到的问题是,当我测试在一个较小的屏幕尺寸的应用程序,该按钮似乎并没有调整,他们熄灭屏幕底部。我怎样才能让他们正确地扩展?我想用DP照顾所有的?
Now the problem I'm having is when I test the app on a smaller screen size, the buttons don't seem to resize and they go off the screen at the bottom. How can I get them to scale properly? I thought using "dp" took care of all that?
我需要计算屏幕尺寸编程?
Do I need to calculate the screen size programatically?
感谢您的建议/帮助!
推荐答案
使用类似下面的布局,以正确嵌套的按钮:
Use something like the layout below to correctly nest the buttons:
<LinearLayout
android:orientation="vertical">
<TextView>Put your Text here</TextView>
<LinearLayout
android:orientation="horizontal"
android:layout_weightSum="2">
<LinearLayout
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weightSum="3"
android:layout_weight="1">
<Button
android:layout_weight="1"/>
<Button
android:layout_weight="1"/>
<Button
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_weightSum="3"
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weight="1">
<Button
android:layout_weight="1"/>
<Button
android:layout_weight="1"/>
<Button
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
然后,每个按键需要0dp的高度(这让重应用和横向扩展的按钮)和宽度填充母的。
Then each button needs height of 0dp (this lets the weight apply and scale the button horizontally) and width of fill parent.
诀窍是,一个子元素的权重将导致被设置为0dp,以填补父元素的weightSum的比例尺寸。
The trick is that the weight of a child element will cause the dimension that is set to 0dp to fill to a percentage of of the weightSum of the parent element.
例如:我有一台带weightSum 3和重量1. 3子按钮的线性布局在高度设置为0dp他们将各自采取的父母为自己的身高高度的1/3。当宽度被设为0dp它们将各自取母体作为自己的宽度的1/3。
Eg i have a linear layout with weightSum 3 and 3 child buttons of weight 1. When height is set to 0dp they will each take 1/3 of the height of the parent as their height. When width is set to 0dp they will each take 1/3 of the width of the parent as their own.
这是根据一个百分比宽度或高度组织的物品的最佳方式。
This is the best way to organise items according to a percentage width or height.
随着如果您有两个按钮中的一个体重2和一个与权重为1,然后1键是家长的高度/宽度的33%,另外66%的家长的高度/宽度的一边。方便的小把戏,并适用于所有的屏幕上。
As an aside if you had two buttons one with weight 2 and one with weight 1 then 1 button would be 33% of the parents height/width and the other 66% of the parents height/width. Handy little trick and works on all screens.
这篇关于我如何获得六个按钮根据屏幕大小来调整?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!