TextView结束并完全居中于imageView。结果集中在一些设备上,但在4.4的情况下,TextView在uperside是center_horizo​​ntal

时间:2015-11-07 14:55:46

标签: textview android-imageview center android-relativelayout xml-layout

对于单行上侧的v4.4设备,Textview只是center_horizo​​ntal。对于v4.1和v5.0,textView完全居中。我在StackOverflow中检查了这些问题,但没有答案解决了这个问题。 请查看我的代码。

single_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="4dp"
        android:layout_margin="4dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:paddingLeft="10dp">
            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:src="@drawable/drawable_oval" />

            <TextView
                android:gravity="center"
                android:paddingBottom="3dp"
                android:paddingTop="3dp"
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/def_total_schedule"
                android:textSize="11sp"
                android:layout_alignBottom="@+id/imageView1"
                android:layout_toRightOf="@+id/imageView1"
                android:layout_toLeftOf="@+id/imageView2"
                android:layout_toStartOf="@+id/imageView2" />

            <TextView
                android:textStyle="bold"
                android:textSize="15sp"
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/imageView1"
                android:text="@string/default_schedule"
                android:gravity="center"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:layout_toLeftOf="@+id/imageView2"
                android:layout_toRightOf="@+id/imageView1"
                android:layout_toEndOf="@+id/imageView1" />

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingRight="10dp"
                android:src="@mipmap/ic_location"
                android:layout_alignTop="@+id/textView1"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_alignBottom="@+id/imageView1" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_alignParentStart="true"
                android:text="@string/sunday"
                android:textColor="@color/icons"
                android:textStyle="bold"
                android:id="@+id/textViewDAY"
                android:layout_alignTop="@+id/imageView1"
                android:layout_alignBottom="@+id/imageView1"
                android:layout_alignLeft="@+id/imageView1"
                android:layout_alignRight="@+id/imageView1"
                android:gravity="center"
                />

            <TextView
                android:textStyle="bold"
                android:textSize="15sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/default_schedule"
                android:id="@+id/textView"
                android:gravity="center"
                android:layout_below="@+id/textView1"
                android:layout_toRightOf="@+id/imageView1"
                android:layout_toLeftOf="@+id/imageView2"
                android:layout_toStartOf="@+id/imageView2" />
        </RelativeLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

最后完成

在这里,我们必须制作内部RelativeLayout并使用属性android:layout_centerInParent =&#34; true&#34;到TextView。

       <RelativeLayout
            android:id="@+id/child_relative_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true">

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:src="@drawable/drawable_oval" />

            <TextView
                android:id="@+id/textViewDAY"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="@string/sunday"
                android:textColor="@color/icons"
                android:textStyle="bold" />

        </RelativeLayout>