我在Android布局上遇到一些问题。在我的一个屏幕上,我希望将四个按钮显示为正方形,两个按钮并排放置,而另外两个则在下面。我查看了如何实现该目标的另一个线程,但是当我说了什么时,它变成了一个完整的烂摊子,根本没有用。
这是我当前屏幕的外观:
这是我当前显示该屏幕的xml代码:
<TableLayout 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=".PedometerActivity" >
<TextView
android:id="@+id/pedometerRecommendationTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/recommendation_pedometer"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Chronometer
android:id="@+id/chronometerChronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chronometer" />
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="26dp" >
</TableRow>
<Button
android:id="@+id/resetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="88dp"
android:layout_span="4"
android:gravity="center"
android:text="@string/reset_steps_button_pedometer" />
<Button
android:id="@+id/startPedometerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start_button_pedometer" />
<Button
android:id="@+id/pausePedometerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pause_button_pedometer" />
<Button
android:id="@+id/finishPedometerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/finish_button_pedometer" />
<TextView
android:id="@+id/stepsTextView"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_span="4"
android:gravity="center_vertical|center_horizontal"
android:text="---"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/seekBarTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/seek_bar_pedometer" />
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="20"
android:layout_span="4" />
<TextView
android:id="@+id/sensitivityTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_span ="4"
android:gravity="center_vertical|center_horizontal"
android:text="@string/sensitivity_bar_pedometer" />
</TableLayout>
谢谢!
您需要将每对buttons
具有LinearLayout
水平方向的包裹,然后再将2包裹LinearLayouts
以LinearLayout
垂直方向的父包裹:
<!- your TableRow above ->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/resetButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_span="4"
android:layout_weight="1"
android:text="@string/reset_steps_button_pedometer" />
<Button
android:id="@+id/startPedometerButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/start_button_pedometer" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/pausePedometerButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/pause_button_pedometer" />
<Button
android:id="@+id/finishPedometerButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/finish_button_pedometer" />
</LinearLayout>
</LinearLayout>
<!- your TextViews below ->
这是结果(对于屏幕截图,我只是将按钮居中并更改了背景色):
关于四个按钮,您可以:
1. 使用相同的固定大小button's width and height
(在这种情况下,您不应使用android:layout_weight
属性)。例如
<Button
android:id="@+id/finishPedometerButton"
android:layout_width="20dp"
android:layout_height="20dp"
android:text="@string/finish_button_pedometer" />
请注意,button
固定大小后,请确保buttons
不会截断的文本。您也可以根据屏幕大小使用不同的大小值。为此,定义每个/res/values-xxx
文件夹的值,它们类似于
<Button
android:id="@+id/finishPedometerButton"
android:layout_width="@dimen/button_width"
android:layout_height="@dimen/button_height"
android:text="@string/finish_button_pedometer" />
2. 使用方形图像作为的背景buttons
。dpi
为/res/drawable-xxx
项目的每个文件夹创建宽度和高度相等的图像,但各不相同。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句