简体   繁体   English

Android:对齐复杂的LinearLayout内容

[英]Android: align complex LinearLayout contents

I've the following Linear layout and been trying align elements by a single vertical baseline as shown on the screenshot 我使用了以下线性布局,并尝试按单个垂直基线对齐元素,如屏幕截图所示

How it looks so far 到目前为止看起来如何

线性布局

Activity xml source 活动xml源

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:clickable="true"
                android:background="@drawable/container_drowshadow_grey"
                android:baselineAligned="false"
                android:padding="8dp">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:id="@+id/first_language"
        android:layout_marginLeft="10dp">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/lang_source_icon"
            android:src="@drawable/ic_flag_en"
            android:contentDescription="@string/language_direction"
            android:layout_gravity="center_horizontal"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="English"
            android:id="@+id/lang_source_text"
            android:gravity="center_vertical"
            android:layout_marginTop="4dp"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="10dp"
            android:textColor="@color/text_source_phrase"/>

    </LinearLayout>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/language_direction"
        android:src="@drawable/ic_arrowlist"
        android:contentDescription="@string/language_direction"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:id="@+id/second_language">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/lang_destination_icon"
            android:src="@drawable/ic_flag_es"
            android:contentDescription="@string/language_direction"
            android:layout_gravity="left"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Espanol"
            android:id="@+id/lang_destination_text"
            android:gravity="center_vertical"
            android:layout_marginTop="4dp"
            android:layout_marginLeft="6dp"
            android:textColor="@color/text_source_phrase"/>
    </LinearLayout>

</LinearLayout>

I want to align all list items by the red line on the screen shot. 我想通过屏幕截图上的红线对齐所有列表项。 How to do this? 这个怎么做? I've tried to play with android:gravity and android:layout_gravity attributes but didn't succeed. 我尝试使用android:gravityandroid:layout_gravity属性,但未成功。

Try this.. give android:layout_weight="0.5" for horizontal two layouts 试试这个..给android:layout_weight="0.5"设置水平两个布局

    <LinearLayout
    android:layout_width="0dp"             //Correction here
        android:layout_weight="0.5"          //Correction here
    android:layout_height="wrap_content"
        android:gravity="left"
        android:id="@+id/first_language"
        android:layout_marginLeft="10dp">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/lang_source_icon"
            android:src="@drawable/ic_flag_en"
            android:contentDescription="@string/language_direction"
            android:layout_gravity="center_horizontal"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="English"
            android:id="@+id/lang_source_text"
            android:gravity="center_vertical"
            android:layout_marginTop="4dp"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="10dp"
            android:textColor="@color/text_source_phrase"/>

    </LinearLayout>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/language_direction"
        android:src="@drawable/ic_arrowlist"
        android:contentDescription="@string/language_direction"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"/>

    <LinearLayout
    android:layout_width="0dp"                //Correction here
        android:layout_weight="0.5"           //Correction here
    android:layout_height="wrap_content"
        android:gravity="right"
        android:id="@+id/second_language">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/lang_destination_icon"
            android:src="@drawable/ic_flag_es"
            android:contentDescription="@string/language_direction"
            android:layout_gravity="left"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Espanol"
            android:id="@+id/lang_destination_text"
            android:gravity="center_vertical"
            android:layout_marginTop="4dp"
            android:layout_marginLeft="6dp"
            android:textColor="@color/text_source_phrase"/>
    </LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM