如何在左侧创建Textview,在同一行中创建右侧的两个按钮?

时间:2012-03-29 10:45:22

标签: android android-layout

在Android中,如何在左侧创建一个TextView,在右侧创建两个同一行的按钮。 哪种布局最适合用于此布局表或相对?

3 个答案:

答案 0 :(得分:1)

您可以使用 LinearLayout 来实现此目标

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:layout_weight="1"
        android:text="a text"/>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button 1">
    </Button>

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button 2">
    </Button>
</LinearLayout>

答案 1 :(得分:1)

对于这种视图,最好使用相对布局。使用android:layout_alignParentRight="true"右对齐,android:layout_alignParentLeft="true"左对齐。 e.g

RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp" >

    <TextView
        android:id="@+id/type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Large Text"
        android:textColor="#76D4F7"
        android:textStyle="bold"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

答案 2 :(得分:0)

最简单的方法是使用前一个答案中指出的LinearLayout(水平方向)。您还可以为每个按钮和textview将“布局权重”下的属性设置为1。然后将所有这些对象的布局宽度设置为0.这将使您能够按比例缩放每个对象水平占用的区域的大小。因此,例如,如果您将Textview上的布局权重设置为2并将每个按钮设置为1,则textview将占用水平空间的50%(2 /(2 + 1 + 1))。这样可以更容易地在不同的设备上缩放对象,并希望开始解决在这种情况下使用哪个最佳布局的问题。

相关问题