开发者

How can I align this layout correctly?

I have two textviews. I want one of them to be left aligned and the other to be right aligned. Pretty much so that it looks like a 2 column table.

<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/profileLayout" android:orientation="vertical">
        <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/experience" android:orientation="horizontal">
            <TextView android:layout_height="wrap_co开发者_运维知识库ntent" android:layout_width="wrap_content" android:id="@+id/experienceLabel" android:textStyle="bold" android:layout_weight="0" android:text="Experience"></TextView>
            <LinearLayout android:id="@+id/linearLayout4" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1"></LinearLayout>
            <TextView android:text="TextView" android:layout_height="wrap_content" android:id="@+id/experienceTextView" android:layout_gravity="right" android:layout_weight="0" android:layout_width="wrap_content"></TextView>
        </LinearLayout>
    </LinearLayout>


<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal">
    <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight="1"/>    
    <TextView android:layout_height="wrap_content" android:layout_weight="0" android:layout_width="wrap_content"/>
</LinearLayout>

Set the weight of the first one to 1 so it will eat up all of the extra space. Set the 2nd oen to wrap content, so it only takes up as much space as it needs.

But why do you have an extra LinearLayout around them? If you have nested LinearLayouts, you should probably be using a RelativeLayout instead.


        <TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/experienceLabel" android:textStyle="bold" android:layout_weight="1" android:text="Experience"></TextView>

        <TextView android:text="TextView" android:layout_height="wrap_content" android:id="@+id/experienceTextView"  android:layout_weight="1" android:layout_width="wrap_content"></TextView>

</LinearLayout>


Use Relative Layout. About Relative Layout Link Example link

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" 
android:layout_height="wrap_content">
<TextView android:layout_height="wrap_content"
    android:layout_width="wrap_content" android:id="@+id/experienceLabel"
    android:textStyle="bold" android:layout_weight="0" android:text="Experience"
    android:layout_alignParentLeft="true"/>
<TextView android:text="TextView" android:layout_height="wrap_content"
    android:id="@+id/experienceTextView" android:layout_gravity="right"
    android:layout_weight="0" android:layout_width="wrap_content"
    android:layout_alignParentRight="true" />

Benefit of using RelativeLayout: it reduces your number of layout. Which will optimize your ui too.


I'm not entirely sure of the exactly layout you want, but you can try this:

<?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="wrap_content"
    android:id="@+id/profileLayout"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:weightSum="2"
        android:id="@+id/experience"
        android:orientation="horizontal">
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:id="@+id/experienceLabel"
            android:textStyle="bold"
            android:layout_weight="1"
            android:text="Experience"/>

        <TextView
            android:text="TextView"
            android:layout_height="wrap_content"
            android:id="@+id/experienceTextView"
            android:gravity="right"
            android:layout_weight="1"
            android:layout_width="wrap_content"/>
    </LinearLayout>
</LinearLayout>

You were using layout_gravity rather than gravity also. layout_gravity tells the parent how you want to be laid out, gravity tells content how to be laid out.

Notice how I have a weightSum in the parent layout to 2 and then set the two children to a weight of 1 each. I also set the parent to fill_parent

You had and extra layout in there too - if you want spacing, set a margin on the second textview or use a view (a simpler object than a LinearLayout)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜