1

我在使用 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>

谢谢!

4

1 回答 1

2

buttons您需要用水平方向包装每一对,LinearLayout然后 2LinearLayouts也应该用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. 使用方形图像作为背景buttonsdpi为项目的每个/res/drawable-xxx文件夹创建具有不同宽度和高度的图像。

于 2014-05-24T18:51:11.213 回答