Android:LinearLayout中元素的对齐方式

时间:2015-04-20 07:54:28

标签: android layout alignment

我目前正在为ListView中的项目构建自定义布局。首先,这是当前的XML文件:

<?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="horizontal"
    android:gravity="right">

    <LinearLayout
    android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_weight="1">

        <TextView
        android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Medium Text"
            android:id="@+id/textGameName"
            android:singleLine="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:id="@+id/textRuleName"
            android:singleLine="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:id="@+id/textLanguageName"
            android:singleLine="true" />
    </LinearLayout>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:id="@+id/imageAction"
        android:src="@mipmap/action_required"
        android:scaleType="center"
        android:layout_gravity="right" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:id="@+id/imageNext"
        android:src="@mipmap/button_list_enter"
        android:scaleType="center"
        android:layout_gravity="right" />

</LinearLayout>

基本上,我想要的是:我希望两个ImageView总是尽可能地在右边,而内部有3个TextViews的LinearLayout将使用整个剩余空间。在android studio编辑器中,我能够获得所需的结果,但是只要我在Android上运行它(我的是4.4.2),LinearLayout就不会展开,按钮就在文本旁边。

3 个答案:

答案 0 :(得分:1)

您可以使用此布局来获得所需的结果。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_toLeftOf="@+id/imageAction"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Medium Text"
            android:id="@+id/textGameName"
            android:singleLine="true" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:id="@+id/textRuleName"
            android:singleLine="true" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:id="@+id/textLanguageName"
            android:singleLine="true" />
    </LinearLayout>
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/imageAction"
        android:src="@mipmap/action_required"
        android:scaleType="center"
        android:layout_toLeftOf="@+id/imageNext" />
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/imageNext"
        android:src="@mipmap/button_list_enter"
        android:scaleType="center"
        android:layout_alignParentRight="true" />
</RelativeLayout>

答案 1 :(得分:1)

这有帮助吗?如果它没有给我一个项目应该如何

的图片
 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="right">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_toLeftOf="@+id/linearLayout2"
        android:layout_toStartOf="@+id/linearLayout2">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Medium Text"
            android:id="@+id/textGameName"
            android:singleLine="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:id="@+id/textRuleName"
            android:singleLine="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:id="@+id/textLanguageName"
            android:singleLine="true" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:id="@+id/linearLayout2">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:id="@+id/imageNext"
            android:src="@drawable/ic_launcher"
            android:scaleType="center"
            android:layout_gravity="right"
            android:layout_weight="1" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:id="@+id/imageAction"
            android:src="@drawable/ic_launcher"
            android:scaleType="center"
            android:layout_weight="1" />
    </LinearLayout>

</RelativeLayout>

答案 2 :(得分:1)

试试这个

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

    <LinearLayout
        android:id="@+id/linLeft"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/linRight"
        android:layout_weight="1"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textGameName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/textRuleName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/textLanguageName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_gravity="left"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/imageAction"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_gravity="right"
            android:scaleType="center"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/imageNext"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_gravity="right"
            android:scaleType="center"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

</RelativeLayout>