我需要将一个TextView放在另一个以parent为中心的TextView下面:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative_row_product"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp" >
<ImageView
android:id="@+id/imageview_product"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginRight="20dp"
android:contentDescription="@string/product" >
</ImageView>
<TextView
android:id="@+id/textview_title_product"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toRightOf="@id/imageview_product"
android:textSize="16sp"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/textview_category_product"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textview_title_product"
android:layout_toRightOf="@id/imageview_product"
android:textColor="@color/gray"
android:textSize="14sp" >
</TextView>
</RelativeLayout>textview_category_product会导致与第一个TextView重叠(似乎忽略了centerInparent标志)。我怎样才能做到这一点呢?非常感谢。
发布于 2013-09-23 00:54:08
试试这个..。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/imageview_product"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginRight="20dp"
android:contentDescription="@string/product" >
</ImageView>
<TextView
android:id="@+id/textview_title_product"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:gravity="center"
android:layout_gravity="center"
android:textStyle="bold" >
</TextView>
</LinearLayout>
<TextView
android:id="@+id/textview_category_product"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:textColor="@color/gray"
android:textSize="14sp" >
</TextView>
</LinearLayout>发布于 2013-09-23 03:13:18
在……里面
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative_row_product"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp" >它应该是
android:layout_height="match_parent"而不是
android:layout_height="wrap_content"android:layout_height="wrap_content“将布局的高度设置为视图所需的最大高度。它不会填满整个屏幕。这样centerInParent就不会集中在你的屏幕上!
此外,在
<TextView
android:id="@+id/textview_title_product"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toRightOf="@id/imageview_product"
android:textSize="16sp"
android:textStyle="bold" >
</TextView>我觉得centerInParent和toRightOf不应该在一起。相反,只在textview_title_product上使用centerInParent,并使用
android:layout_toLeftOf="@id/textview_title_product"在你的imageView中
希望它能有所帮助:)
发布于 2013-09-23 12:12:55
// try this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative_row_product"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp" >
<ImageView
android:id="@+id/imageview_product"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginRight="20dp"
android:contentDescription="@string/product" >
</ImageView>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/textview_title_product"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="First TextView"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/textview_category_product"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/darker_gray"
android:text="Second TextView"
android:textSize="14sp" >
</TextView>
</LinearLayout>
</LinearLayout>https://stackoverflow.com/questions/18946184
复制相似问题