如何使用6个按键,如Windows瓷砖打造布局 [英] How to create layout with 6 buttons like windows tiles
问题描述
我试图创建6个按键,可自动适应屏幕大小为windows phone的瓷砖的布局。在code我动态创建按钮6,2线,但该按钮应符合屏幕尽显后者的大小。我该怎么处理?
I'm trying to create a layout with 6 buttons that automatically adapt to the screen size as the tiles of windows phone. In the code I create dynamically the 6 button, 2 for line but the button should fit the size of the screen filling the latter. how can I proceed?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/conv_up" />
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/conv_up"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/conv_up"
/>
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/conv_up"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/conv_up"
/>
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/conv_up"
/>
</LinearLayout>
推荐答案
我会使用一个垂直的LinearLayout
三排同等重量的儿童,每行是一个水平的LinearLayout
具有相同的权重,这将确保整个区域被填充的两个孩子。对于六个按钮的表现不应该是一个问题。
I'd use a vertical LinearLayout
with three rows of same weight as children, each row being a horizontal LinearLayout
having two children of same weights, which will make sure the full area is filled. For six buttons performance shouldn't be an issue.
如果性能是一个问题,可以使行作为 RelativeLayout的
和使用支柱分裂成两半和定位的基础上,这两个孩子。
If performance is a concern, you can make the rows as RelativeLayout
s and use a strut to split in half and position the two children based on that.
当我说的支撑的,我的意思是这样的:
When I say a strut, I mean this:
<View android:id="@+id/strut"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true"/>
更新:
既然你想的的LinearLayout
S,这里是你如何可以用高度和宽度处理:
Update:
Since you're trying the LinearLayout
s, here's how you can deal with the heights and widths:
父的LinearLayout
可以有:
android:layout_width="match_parent"
android:layout_height="match_parent"
三个的LinearLayout
的孩子有:
android:layout_width="match_parent"
android:layout_height="0dip"
在按钮
旨意有:
android:layout_width="0dip"
android:layout_height="match_parent"
正如你可以看到,我们有 0dip
对于重量施加的财产(无论是身高,如果父母是垂直的导向,或宽度,如果父母是水平导向) ,这将需要成长以填充在该空间
As you can notice, we have 0dip
for the property that weight is applied on (either on height if parent is vertical oriented, or width if parent is horizontal oriented), which will need to grow to fill in the space.
下面是完整的XML(按钮不包括可绘,可以随意添加你的):
Here's the full XML (buttons don't include drawables, so feel free to add yours):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="1" >
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="1" >
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="1" >
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
和结果:
And the result:
这篇关于如何使用6个按键,如Windows瓷砖打造布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!