LinearLayout“center_vertical”

时间:2015-12-13 15:11:08

标签: android android-layout

我正在尝试将一个textview放在ListView项目的中间。

所以我创建了一个LinearLayout来处理圆圈和文本视图。

圆形是一种形状:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
    <solid android:color="@color/accentColor"></solid>
</shape>

我希望TextView垂直居中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="72dp"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="16dp"
        android:gravity="center_vertical">

        <TextView
            android:id="@+id/vplan_list_item_hour"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@drawable/rectangle"
            android:gravity="center"
            android:text="1"
            android:textSize="25sp" />
    </LinearLayout>
    ...
</RelativeLayout>

它在android studio的设计选项卡中看起来正确:

enter image description here

但是当我运行应用程序时,它看起来像这样:

enter image description here

有没有人知道如何解决这个问题?

5 个答案:

答案 0 :(得分:0)

使用形状作为背景

可绘制

中的

shape_rounded.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
  android:shape="rectangle" >

  <corners android:radius="24dp"/>

  <solid android:color="#A5A5A5" />

</shape>

<强>布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="72dp" >

<TextView
    android:id="@+id/vplan_list_item_hour"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:background="@drawable/shape_rounded"
    android:gravity="center"
    android:text="1"
    android:layout_margin="16dp"
    android:textSize="25sp" />
</RelativeLayout>

答案 1 :(得分:0)

您是否尝试使用RelativeLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="72dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="16dp">

        <TextView
            android:id="@+id/vplan_list_item_hour"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_centerVertical="true"
            android:background="@drawable/rectangle"
            android:gravity="center"
            android:text="1"
            android:textSize="25sp" />

        ...

    </RelativeLayout>

</RelativeLayout>

答案 2 :(得分:0)

我试着简化你的布局。我在Genymotion中测试过它(包括Android 4.4) - 就像一个魅力。试一试。

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="72dp">
    <TextView
        android:id="@+id/vplan_list_item_hour"
        android:layout_centerVertical="true"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginStart="16dp"
        android:background="@drawable/shape_rounded"
        android:gravity="center"
        android:textSize="25sp"
        android:text="1" />
</RelativeLayout>

希望,这有帮助

答案 3 :(得分:0)

尝试以下代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="72dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/vplan_list_item_hour"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/rectangle"
        android:padding = "7dp"
        android:gravity="center"
        android:text="1"
        android:textSize="25sp" />
</LinearLayout>
...

答案 4 :(得分:0)

您可以在文本视图中同时尝试android:layout_gravity="center"<TextView android:id="@+id/vplan_list_item_hour" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@drawable/rectangle" android:padding = "7dp" android:gravity="center" android:layout_gravity="center" android:text="1" android:textSize="25sp" />

    android:text="New Text" 

或者您可以为LinearLayout设置背景并再次检查textview的位置。

相关问题