[英]Using different XML layouts for different Android screens?
I'm a beginner to Android, and I'm trying to get a couple of buttons to line up perfectly. 我是Android的初学者,我正在尝试使几个按钮完美对齐。 I've done so right here:
我已经在这里完成了:
And here's the code: 这是代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Scientific">
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="63dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/button1"
android:layout_width="63dip"
android:layout_height="63dip"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="1" />
<Button
android:id="@+id/Button01"
android:layout_width="63dip"
android:layout_height="63dip"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_toRightOf="@+id/button1"
android:text="1" />
<Button
android:id="@+id/Button02"
android:layout_width="63dip"
android:layout_height="63dip"
android:layout_alignBaseline="@+id/Button01"
android:layout_alignBottom="@+id/Button01"
android:layout_toRightOf="@+id/Button01"
android:text="1" />
<Button
android:id="@+id/Button04"
android:layout_width="63dip"
android:layout_height="63dip"
android:layout_alignBaseline="@+id/Button03"
android:layout_alignBottom="@+id/Button03"
android:layout_toRightOf="@+id/Button03"
android:text="1" />
<Button
android:id="@+id/Button03"
android:layout_width="63dip"
android:layout_height="63dip"
android:layout_alignBaseline="@+id/Button02"
android:layout_alignBottom="@+id/Button02"
android:layout_toRightOf="@+id/Button02"
android:text="1" />
</RelativeLayout>
The problem is, this code does not position 5 buttons perfectly across the bottom of the screen across different screen sizes : 问题是,此代码无法在不同屏幕尺寸的屏幕底部完美地放置5个按钮:
Reading up on the official Android developers site, I gathered that I might need different activity layouts for small, normal, large, and xlarge screen sizes. 在Android官方开发人员网站上阅读后,我发现对于小,标准,大和xlarge屏幕尺寸,我可能需要不同的活动布局。 I also read something about using layout_weight in LinearLayout.
我还阅读了有关在LinearLayout中使用layout_weight的内容。 What's the right way to go?
正确的方法是什么? Could I have some example code, if possible?
如果可以的话,我可以提供一些示例代码吗?
EDIT: 编辑:
I would also like the buttons to scale in height so that the same amount of buttons can fit vertically across devices. 我还希望按钮可以按高度缩放,以使相同数量的按钮可以在设备上垂直放置。
Use a LinearLayout for buttons use layout_weight 对按钮使用LinearLayout使用layout_weight
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Scientific">
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="63dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_height="63dip"
android:layout_width="0dp"
android:layout_weight="1"
android:text="1" />
<Button
android:id="@+id/Button01"
android:layout_height="63dip"
android:layout_width="0dp"
android:layout_weight="1"
android:text="1" />
<Button
android:id="@+id/Button02"
android:layout_height="63dip"
android:layout_width="0dp"
android:layout_weight="1"
android:text="1" />
<Button
android:id="@+id/Button04"
android:layout_height="63dip"
android:layout_width="0dp"
android:layout_weight="1"
android:text="1" />
<Button
android:id="@+id/Button03"
android:layout_width="0dp"
android:layout_height="63dip"
android:layout_weight="1"
android:text="1" />
</LinearLayout>
</RelativeLayout>
Snap 捕捉
try adding same layout_weight
to all your buttons
尝试为所有
buttons
添加same layout_weight
<Button
android:id="@+id/Button03"
android:layout_width="63dip"
android:layout_weight="1.0"
android:layout_height="63dip"
android:layout_alignBaseline="@+id/Button02"
android:layout_alignBottom="@+id/Button02"
android:layout_toRightOf="@+id/Button02"
android:text="1" />
PS : layout_weight
does not work with Relative Layout
, you would have to use a linear layout with orientation: horizontal
PS:
layout_weight
不适用于“ Relative Layout
,您必须使用linear layout with orientation: horizontal
的linear layout with orientation: horizontal
When you use android:layout_weight
you should set android:layout_width="0dp"
(in case when vertical orientation - android:layout_height="0dp"
). 当您使用
android:layout_weight
,应设置android:layout_width="0dp"
(以垂直方向显示android:layout_height="0dp"
)。
Depend on what screen size you support, you need to specify the appropriate screen layouts. 根据您支持的屏幕尺寸,您需要指定适当的屏幕布局。 I think you should start with this article first: http://developer.android.com/guide/practices/screens_support.html
我认为您应该首先从这篇文章开始: http : //developer.android.com/guide/practices/screens_support.html
res/layout/my_layout.xml // layout for normal screen size ("default")
res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation
With some insight from @Raghunandan, I was able to achieve, essentially, a table of buttons that scale up and down the same way, by using multiple LinearLayouts, each with layout_weight of 1, inside another LinearLayout. 通过@Raghunandan的一些见解,我基本上可以通过在另一个LinearLayout中使用多个LinearLayouts(每个layout_weight为1)来以相同的方式实现按比例缩放按钮的表格。 Then, inside each LinearLayout, I placed several buttons, also with layout_weight of 1
然后,在每个LinearLayout中,放置几个按钮,layout_weight为1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.