Center textview, when other textview it's null (or dont have text)
I followed this tutorial, for now it's working nice, but i only have a problem, i need to center vertically the "Title" textview, when "Detail" textview开发者_如何转开发 text it's null or empty, something like this, any help appreciated, thanks
Here it's the custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/list"
android:layout_width="1px"
android:layout_height="1px"
android:layout_weight="1">
</ListView>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/img"
android:layout_width="50dp"
android:layout_height="60dp"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<TextView
android:id="@+id/detail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/img"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:singleLine="true"
android:ellipsize="marquee" />
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/img"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_above="@id/detail"
android:layout_alignWithParentIfMissing="true"
android:gravity="center_vertical"
android:textColor="#ffffffff"
android:textSize="24sp" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
You use android:layout_gravity
to center the actual text view and android:gravity
to center the things inside the text view (like the text).
Example XML:
<TextView android:id="@+id/some_id"
android:text="Example text"
android:layout_height="70dp"
android:layout_width="wrap_content"
android:gravity="center_vertical" />
精彩评论