views:

39

answers:

2
<LinearLayout android:layout_width="fill_parent" 
            android:orientation="horizontal"
            android:layout_height="wrap_content">
<LinearLayout android:id="@+id/LinearLayout01" 
            android:layout_width="wrap_content" 
            android:orientation="horizontal"
            android:gravity="center_horizontal"
            android:layout_height="wrap_content">
    <TextView android:text="2" 
            android:id="@+id/numberone" 
            android:layout_width="wrap_content" 
            android:textStyle="bold"
            android:textSize="24px"
            android:layout_height="wrap_content"></TextView>
    <TextView android:text="x" 
            android:id="@+id/operation" 
            android:textStyle="bold"
            android:textSize="24px"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"></TextView>

    <TextView android:text="5" 
            android:id="@+id/numbertwo" 
            android:textStyle="bold"
            android:textSize="24px"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"></TextView>
  </LinearLayout>
 <TextView android:text="5" 
            android:id="@+id/numbertwo" 
            android:textStyle="bold"
            android:textSize="24px"
            android:gravity="left"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"></TextView>
 </LinearLayout>

I try to set the first three Textfields in the center, and the last TextField on the left. But all TextViews are displayed on the right.

But i set the gravity tag.

Thanks for your help, Martin

A: 

Yes , you set gravity but issue is that it will apply to position in your LinearLayout01. Since your LinearLayout01 has width set to wrap_content, it will occupy just place needed to display 2x5. If you change that to fill_parent, you will see 2x5 in the center, but not the other 5.

For more freedom one option is using RelativeLayout, having other layout types (Linear, Relative ...) as children.

Desiderio
A: 
<RelativeLayout android:layout_width="fill_parent" 
            android:orientation="horizontal"
            android:layout_height="wrap_content">
<LinearLayout android:id="@+id/LinearLayout01" 
            android:layout_width="wrap_content" 
            android:orientation="horizontal"
            android:gravity="center_horizontal"
            android:layout_height="wrap_content">
    <TextView android:text="2" 
            android:id="@+id/numberone" 
            android:layout_width="wrap_content" 
            android:textStyle="bold"
            android:textSize="24px"
            android:layout_height="wrap_content"></TextView>
    <TextView android:text="x" 
            android:id="@+id/operation" 
            android:textStyle="bold"
            android:textSize="24px"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"></TextView>

    <TextView android:text="5" 
            android:id="@+id/numbertwo" 
            android:textStyle="bold"
            android:textSize="24px"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"></TextView>
  </LinearLayout>
 <TextView android:text="5" 
            android:id="@+id/numbertwo" 
            android:textStyle="bold"
            android:textSize="24px"
            android:gravity="left"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"></TextView>
 </RelativeLayout>

Mhm, I tried to use RelativeLayout instead of LinearLayout, but I didn't work.

Martin Kapfhammer
Yes, but when using RelativeLayout you can say which element is to the left (Align parent left), and which one is maybe centered in the parent layout. So, you can define relative positions one to the other. I see that you just replaced LinearLayout with RelativeLayout, and have orientation horizontal (?). You should do that using Eclipse editor for Android layouts, not through changing xml source directly. It is easier and more useful.
Desiderio
cool, it works now. Thanks!
Martin Kapfhammer
I am glad that you managed to get it.
Desiderio
another question: I have to add one textview in the code of my application. How can i set: android:layout_alignParentRight="true"?
Martin Kapfhammer
already found it: RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
Martin Kapfhammer