How to Configure Widgets in the Center Vertically on LinearLayout
source link: https://www.codesd.com/item/how-to-configure-widgets-in-the-center-vertically-on-linearlayout.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How to Configure Widgets in the Center Vertically on LinearLayout
I want to set the Widgets
at center_vertical
on my horizontal
LinearLayout
and set the last Textview
at right side of parent layout.
I tried it out but I'm having a hard time. I included my code below.
xml layout:
<?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="48dp"
android:orientation="horizontal"
android:background="@drawable/list_selector">
<ImageView
android:id="@+id/icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:contentDescription="@string/desc_list_item_icon"
android:src="@drawable/image1"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/icon"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="@color/colorPrimary"
android:layout_centerVertical="true"
android:text="Hello world"
android:paddingRight="40dp"/>
<TextView android:id="@+id/counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/counter_bg"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="8dp"
android:textColor="@color/counter_text_color"/>
</LinearLayout>
Here is what I'm trying to produce:
android:layout_centerVertical
doesn't do anything within a LinearLayout (that's only used in RelativeLayouts)
You'll want to use android:layout_gravity="center_vertical"
on the each of the children, or android:gravity="center_vertical"
on the LinearLayout
EDIT: And one way to get your last view to stay on the right side of the layout would be to make your title view have the attribute android:weight="1"
to make its width expand to fill the layout
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK