对齐 2 个 TextViews 一个在另一个下面但以它们的相对父为中心
Align 2 TextViews one under other but centered to their relative parent
我试图将一个 TextView 置于另一个 TextView 的中心(它正在居中),但我得到了这个
这 2 个项目没有正确对齐(如我所愿)
我正在寻找这样的东西:
您可以看到两个项目都居中,而不仅仅是较大的那个
我的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="@+id/r1"
android:clickable="true"
android:background="?android:selectableItemBackground"
android:layout_height="?android:attr/listPreferredItemHeight"
>
<ImageButton
android:id="@+id/status"
android:layout_width="wrap_content"
android:background="@null"
android:paddingRight="6dp"
android:layout_height="fill_parent"
android:src="@drawable/download"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/descricao"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:text="Texto de baixo"
android:singleLine="true"
android:typeface="serif"
android:textSize="10sp"
android:layout_centerVertical="true"
android:layout_below="@+id/titulo"
android:layout_toRightOf="@+id/status" />
<TextView
android:id="@+id/titulo"
android:textStyle="bold"
android:text="Text Title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textSize="16sp"
android:layout_toRightOf="@+id/status" />
</RelativeLayout>
编辑
谢谢 Der Golem,你的回答成功了,但我遇到了另一个问题,当文本超过一行时,有没有办法不把它隐藏在下面的文本后面,然后重新对齐?
你是nearly there
:看图
是红线a generic View
(让我们给它一个id vwDummy
),对齐到行的中心(android:layout_centerInParent="true"
)。
设其尺寸为 0dp 和 0dp.
然后把标题放在上面 (android:layout_above="@id/vwDummy"
).
并把下面的文字放在下面(android:layout_below="@id/vwDummy"
)。
只需将父布局的 layout_height 从 "?android:attr/listPreferredItemHeight"
更改为 wrap_content
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="@+id/r1"
android:clickable="true"
android:background="?android:selectableItemBackground"
android:layout_height="wrap_content"
>
<ImageButton
android:id="@+id/status"
android:layout_width="wrap_content"
android:background="@null"
android:paddingRight="6dp"
android:layout_height="fill_parent"
android:src="@drawable/download"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/descricao"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:text="Texto de baixo"
android:singleLine="true"
android:typeface="serif"
android:textSize="10sp"
android:layout_centerVertical="true"
android:layout_below="@+id/titulo"
android:layout_toRightOf="@+id/status" />
<TextView
android:id="@+id/titulo"
android:textStyle="bold"
android:text="Text Title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textSize="16sp"
android:layout_toRightOf="@+id/status" />
</RelativeLayout>
我试图将一个 TextView 置于另一个 TextView 的中心(它正在居中),但我得到了这个
这 2 个项目没有正确对齐(如我所愿)
我正在寻找这样的东西:
您可以看到两个项目都居中,而不仅仅是较大的那个
我的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="@+id/r1"
android:clickable="true"
android:background="?android:selectableItemBackground"
android:layout_height="?android:attr/listPreferredItemHeight"
>
<ImageButton
android:id="@+id/status"
android:layout_width="wrap_content"
android:background="@null"
android:paddingRight="6dp"
android:layout_height="fill_parent"
android:src="@drawable/download"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/descricao"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:text="Texto de baixo"
android:singleLine="true"
android:typeface="serif"
android:textSize="10sp"
android:layout_centerVertical="true"
android:layout_below="@+id/titulo"
android:layout_toRightOf="@+id/status" />
<TextView
android:id="@+id/titulo"
android:textStyle="bold"
android:text="Text Title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textSize="16sp"
android:layout_toRightOf="@+id/status" />
</RelativeLayout>
编辑
谢谢 Der Golem,你的回答成功了,但我遇到了另一个问题,当文本超过一行时,有没有办法不把它隐藏在下面的文本后面,然后重新对齐?
你是nearly there
:看图
是红线a generic View
(让我们给它一个id vwDummy
),对齐到行的中心(android:layout_centerInParent="true"
)。
设其尺寸为 0dp 和 0dp.
然后把标题放在上面 (android:layout_above="@id/vwDummy"
).
并把下面的文字放在下面(android:layout_below="@id/vwDummy"
)。
只需将父布局的 layout_height 从 "?android:attr/listPreferredItemHeight"
更改为 wrap_content<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="@+id/r1"
android:clickable="true"
android:background="?android:selectableItemBackground"
android:layout_height="wrap_content"
>
<ImageButton
android:id="@+id/status"
android:layout_width="wrap_content"
android:background="@null"
android:paddingRight="6dp"
android:layout_height="fill_parent"
android:src="@drawable/download"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/descricao"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:text="Texto de baixo"
android:singleLine="true"
android:typeface="serif"
android:textSize="10sp"
android:layout_centerVertical="true"
android:layout_below="@+id/titulo"
android:layout_toRightOf="@+id/status" />
<TextView
android:id="@+id/titulo"
android:textStyle="bold"
android:text="Text Title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textSize="16sp"
android:layout_toRightOf="@+id/status" />
</RelativeLayout>