我在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
的相等宽度和高度的图像。