编辑布局文件

时间:2012-09-27 06:48:29

标签: android layout button

我正在尝试编辑布局文件,以便两个按钮并排显示而不是一个在另一个之上。以下是布局文件中的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="225dp"
android:layout_height="wrap_content"
android:background="@color/dark_yellow"
android:orientation="vertical"
android:padding="12dip" >

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingBottom="12dip"
android:text="@string/dialog_text"
android:textColor="@color/light_yellow"
android:textSize="@dimen/font_medium" />

<Button
android:id="@+id/btn_go_to_new_App"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Go"
android:text="@string/btn_go" />

<Button
android:id="@+id/btn_stay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="stay"
android:text="@string/btn_stay_at_lifecycle" />

</LinearLayout>

截至目前,btn_go出现在btn_stay_at_lifecycle之上。我需要做些什么才能使btn_stay_at_lifecycle右侧显示btw_go

谢谢!

5 个答案:

答案 0 :(得分:3)

您的LinearLayout定义为vertical,因此您添加的每个项目都将垂直整理。

如果您需要并排放置某些商品,请将其与LinearLayout中的商品android:orientation="horizontal"括起来。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="225dp"
    android:layout_height="wrap_content"
    android:background="@color/dark_yellow"
    android:orientation="vertical"
    android:padding="12dip" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:paddingBottom="12dip"
        android:text="@string/dialog_text"
        android:textColor="@color/light_yellow"
        android:textSize="@dimen/font_medium" />

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

        <Button
            android:id="@+id/btn_go_to_new_App"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="Go"
            android:text="@string/btn_go" />

        <Button
            android:id="@+id/btn_stay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="stay"
            android:text="@string/btn_stay_at_lifecycle" />

    </LinearLayout>

</LinearLayout>

The default value of orientation of a LinearLayout is "horizontal"。因此,即使您未在内部android:orientation中指定LinearLayout,它也会并排排列其内容。

答案 1 :(得分:3)

使用以下内容:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="225dp"
android:layout_height="wrap_content"
android:background="@color/dark_yellow"
android:orientation="vertical"
android:padding="12dip" >

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingBottom="12dip"
android:text="@string/dialog_text"
android:textColor="@color/light_yellow"
android:textSize="@dimen/font_medium" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btn_go_to_new_App"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="Go"
android:text="@string/btn_go" />

<Button
android:id="@+id/btn_stay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="stay"
android:text="@string/btn_stay_at_lifecycle" />
</LinearLayout>
</LinearLayout>

答案 2 :(得分:3)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="225dp"
android:layout_height="wrap_content"
android:background="@color/dark_yellow"
android:orientation="vertical"
android:padding="12dip" >

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingBottom="12dip"
android:text="@string/dialog_text"
android:textColor="@color/light_yellow"
android:textSize="@dimen/font_medium" />


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

    <Button
        android:id="@+id/btn_go_to_new_App"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="Go"
        android:text="@string/btn_go" />

    <Button
        android:id="@+id/btn_stay"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="stay"
        android:text="@string/btn_stay_at_lifecycle" />
</LinearLayout>

</LinearLayout>

答案 3 :(得分:3)

试试这个

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="225dp"
    android:layout_height="wrap_content"
    android:background="@color/dark_yellow"
    android:orientation="vertical"
    android:padding="12dip" >

    <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:paddingBottom="12dip"
    android:text="@string/dialog_text"
    android:textColor="@color/light_yellow"
    android:textSize="@dimen/font_medium" />

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

    <Button
    android:id="@+id/btn_go_to_new_App"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:onClick="Go"
    android:text="@string/btn_go" />

    <Button
    android:id="@+id/btn_stay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:onClick="stay"
    android:text="@string/btn_stay_at_lifecycle" />
     </LinearLayout>
    </LinearLayout>

答案 4 :(得分:1)

尝试

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="225dp"
android:layout_height="wrap_content"
android:background="@color/dark_yellow"
android:orientation="vertical"
android:padding="12dip" >

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingBottom="12dip"
android:text="@string/dialog_text"
android:textColor="@color/light_yellow"
android:textSize="@dimen/font_medium" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">

       <Button
    android:id="@+id/btn_go_to_new_App"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight=".5"
    android:onClick="Go"
    android:text="@string/btn_go" />

    <Button
    android:id="@+id/btn_stay"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight=".5"
    android:onClick="stay"
    android:text="@string/btn_stay_at_lifecycle" />

</LinearLayout>
    </LinearLayout>