Reputation: 4595
I have a Spinner and a TextView in my LinearLayour.
I am trying to align the Spinner and a TextView vertically so that the center of both in on the same line:
This is my XML for that element:
<LinearLayout
android:id="@+id/dropdownlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center" />
<TextView
android:id="@+id/ok1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:background="@drawable/white_box"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="@string/ok"
android:textColor="@android:color/black"
android:textSize="@dimen/normale" />
</LinearLayout>
Tried this code as well, same result:
<RelativeLayout
android:id="@+id/dropdownlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:gravity="center" />
<TextView
android:id="@+id/ok1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/spinner1"
android:background="@drawable/white_box"
android:gravity="center"
android:padding="10dp"
android:text="@string/ok"
android:textColor="@android:color/black"
android:textSize="@dimen/normale" />
</RelativeLayout>
Upvotes: 4
Views: 5404
Reputation: 4593
You should stick to the linear layout and try the following on the TextView:
android:layout_gravity="center_vertical"
and then in your TextView you should change
android:layout_height="fill_parent"
into
android:layout_height="wrap_content"
Upvotes: 1
Reputation: 6485
Well note that you can't use RelativeLayout with Gravity or Layout_gravity
an example of how i do things. This works perfectly (it's at the right of a list view cell centerred verticaly)
<RelativeLayout
android:id="@+id/right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="4dp" >
<TextView
android:id="@+id/annuaire_distance_etablissement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="4dp"
android:layout_toLeftOf="@+id/arrow_img"
android:singleLine="true"
android:textColor="@color/clr_main_green"
android:textSize="12sp" />
<ImageView
android:id="@+id/arrow_img"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="4dp"
android:src="@drawable/icone_fleche" />
</RelativeLayout>
Upvotes: 3
Reputation: 17429
You can simple wrap the Spinner
and TextView
inside a LinearLayout
like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
Remove the row
android:layout_toRightOf="@+id/spinner1"
and if you want you can make relative the LinearLayout
(adding the layout_below, layout_above, etc)
Then, inside both TextView
and Spinner
add:
android:layout_gravity="center_vertical"
Upvotes: 2
Reputation: 6925
Try this in Relative Layout like this
[<RelativeLayout
android:id="@+id/dropdownlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="250dp"
android:gravity="center"
android:background="@android:drawable/btn_dropdown"
android:layout_centerVertical="true"
android:layout_height="40dp"
android:layout_gravity="center" />
<TextView
android:id="@+id/ok1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/white_box"
android:gravity="center"
android:layout_centerVertical="true"
android:paddingLeft="10dp"
android:layout_toRightOf="@+id/spinner1"
android:paddingRight="10dp"
android:text="@string/ok"
android:textColor="@android:color/black"
android:textSize="@dimen/normale" />
</RelativeLayout>]
Upvotes: 2