[英]android center textview in layout
我有一個帶有自定義行的列表視圖。 該行中有以下項目:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView1"
android:layout_width="10dp"
android:layout_height="fill_parent"
android:focusable="false"
android:scaleType="fitXY">
</ImageView>"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textsll"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:layout_marginLeft="5dp">
<TextView android:id="@+id/textView1"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:maxLines="1"
android:ellipsize="end">"
</TextView>
<TextView android:id="@+id/textView2"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_alignParentBottom="true"
android:layout_below="@+id/textView1"
android:maxLines="1"
android:ellipsize="end">
</TextView>
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textsll2"
android:layout_gravity="center_vertical|right"
android:layout_marginLeft="10dp">"
<TextView android:id="@+id/textView3"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:textColor="#000000"
android:paddingBottom="5dp"
android:maxLines="1"
android:ellipsize="end"
android:gravity="center_vertical"
android:background="#87EB81">
</TextView>
</LinearLayout>
</LinearLayout>
如果兩個文本視圖都具有某些內容,則它們在行中(幾乎)垂直居中。
但是,如果下面的textview為空,我會調用:
holder.textView2.setVisibility(View.INVISIBLE);
holder.textView.setGravity(Gravity.CENTER_VERTICAL);
仍然,上方的textview進入該行的頂部。 為什么?
如果將其設置為View.GONE,則會得到此結果,但我不希望更改行的高度:
使用android:layout_weight
設置主要LinearLayout的所有子元素以確保相同。
使用LinearLayout作為textView1和textView2的父級。 使用Use android:layout_weight
來使每個尺寸都相同。
使用android:gravity="center_vertical|left"
定義一個TextView然后,如果您不想顯示第二個TextView,請將其可見性設置為View.GONE
。
也許這可以解決您的問題,
嘗試為您的RelativeLayout的高度固定一些值,例如-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textsll"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:layout_marginLeft="5dp">
更新:
在RelativeLayout
,視圖與其父視圖, RelativeLayout
本身或其他視圖對齊。 例如,我們聲明描述與RelativeLayout
的底部對齊,標題位於描述上方,並錨定在父級的頂部。 有了GONE描述, RelativeLayout
不知道標題底邊的位置。 要解決此問題,您可以使用一個非常特殊的布局參數,稱為layout_alignWithParentIfMissing 。
當缺少約束目標時,此boolean
參數只是告訴RelativeLayout
使用其自身的邊緣作為錨點。 例如,如果將視圖放置在GONE視圖的右側,並將alignWithParentIfMissing
設置為true,則RelativeLayout
會將視圖錨定在其左邊緣。 在我們的示例中,使用alignWithParentIfMissing
將使RelativeLayout
將標題的底部與其自身的底部對齊。
有關更多信息,請參見布局技巧:創建有效的布局
你可以在你有可能RelativeLayout
通過設置layout_below
和layout_above
通過與上級路線的幫助layout_centerInParent
並設置值的真layout_centerHorizontal
希望這會起作用。
嘗試使用:
<TextView
...
android:layout_height="?android:attr/listPreferredItemHeight"
...
/>
在your_list_item.xml中。 並使用View.GONE
要解決您的問題,您可以做的是將LinearLayout填充為Parent。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
並使用View.GONE
這將使行高相同。
如果您在“世界最佳汽車”之后發現差距,請嘗試使用“相對布局”,並將最后一個布局與底部對齊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.