Android布局 - 无法获取要显示的按钮和文本

时间:2012-09-13 08:22:02

标签: android layout

我在这里遇到问题,我对Android上的布局不熟悉,我不能按照自己的意愿去做这个布局。

我希望它显示如下:

[Button][Button]
Text Filled Here
Text Filled Here
Text Filled Here
Text Filled Here
Text Filled Here

TextView通过我的messagehelper填充,将文本发送到UI

最终,我的代码将遍历从Web服务检索到的对象列表,并将它们全部显示在自己的行上,并可选择按下一个并在地图上查看它。

目前,我只能分别显示两个按钮或文本,或者同一行中的所有三个按钮,而不是第一行的两个按钮和向下填充的文本!!

这是我的代码:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/buttonLayout" 
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_weight="1">
    <Button
        android:id="@+id/vehicleButton"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:text="Refresh"
        android:onClick="getVehicles" />
    <Button
        android:id="@+id/logoutButton"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:text="Logout"
        android:onClick="logout" />
    </LinearLayout>

    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/textLayout" 
        android:layout_height="wrap_content"
        android:layout_width="fill_parent">
    <TextView
        android:id="@+id/textView1"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>

</LinearLayout>

4 个答案:

答案 0 :(得分:2)

默认情况下,LinearLayout方向为horizontal,您需要将LinearLayout上的第1和第3 xml方向更改为vertical

使用android:orientation="vertical"

答案 1 :(得分:0)

您可以简化布局:

编辑:如果“向下填充”表示您希望文本位于按钮下方:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >


    <Button
        android:id="@+id/vehicleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_weight="1"
        android:onClick="getVehicles"
        android:text="Refresh" />

    <Button
        android:id="@+id/logoutButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/vehicleButton"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@id/vehicleButton"
        android:layout_weight="1"
        android:onClick="logout"
        android:text="Logout" />


    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="false"
        android:layout_alignParentTop="false"
        android:layout_below="@id/vehicleButton"
        android:layout_weight="1"
        android:text="Test"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

答案 2 :(得分:0)

为了将textview底部放在彼此底部,使用方向为垂直。

代码将是这个

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/buttonLayout" 
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_weight="1">
<Button
    android:id="@+id/vehicleButton"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_weight="1"
    android:text="Refresh"
    android:onClick="getVehicles" />
<Button
    android:id="@+id/logoutButton"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_weight="1"
    android:text="Logout"
    android:onClick="logout" />
</LinearLayout>

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textLayout" 
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:orientation="vertical"

>
<TextView
    android:id="@+id/textView1"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_weight="1"
    android:textAppearance="?android:attr/textAppearanceMedium" />
 <TextView
    android:id="@+id/textView1"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_weight="1"
    android:textAppearance="?android:attr/textAppearanceMedium" />

 <TextView
    android:id="@+id/textView1"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_weight="1"
    android:textAppearance="?android:attr/textAppearanceMedium" />


<TextView
        android:id="@+id/textView1"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceMedium" />
 <TextView
        android:id="@+id/textView1"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

</LinearLayout>

答案 3 :(得分:0)

试试这个......

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

    </LinearLayout>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>