【问题标题】:TextView contents not centering correctly on android 4.2.2TextView 内容在 android 4.2.2 上未正确居中
【发布时间】:2014-05-06 12:55:39
【问题描述】:

右侧的图像 (2.3.3) 显示了我想要的视图,但是我无法让它在 4.2.2 上看起来正确。请注意 textview 中的文本如何不垂直居中。我在模拟器和 Galaxy S4 上都试过了。此外,XML 文件的 eclipse ADT 预览显示它正确居中。

视图用于列表视图。

如何在我的 textview 中将文本居中以使其在所有 android 版本上看起来都一样?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:padding="16dp" >

    <ImageView android:id="@+id/fylke_vapen"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:gravity="center"

        android:scaleType="fitCenter"

        android:src="@drawable/fylke_buskerud" />

    <TextView android:id="@+id/fylke_navn"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/fylke_vapen"
        android:layout_alignBottom="@id/fylke_vapen"
        android:layout_alignTop="@id/fylke_vapen"
        android:gravity="center_vertical"

        android:paddingLeft="16dp"

        android:textSize="24sp"
        android:maxLines="1"
        android:text="Placeholder text" />

    <CheckBox android:id="@+id/star"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@id/fylke_vapen"
        android:layout_alignBottom="@id/fylke_vapen"
        android:gravity="center"

        android:button="@drawable/star_checkbox"
        android:focusable="false" /> 

</RelativeLayout>

【问题讨论】:

    标签: android android-layout android-listview textview


    【解决方案1】:

    您应该删除android:layout_alignBottomandroid:layout_alignTop 属性。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/parent"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:padding="16dp" >
    
        <ImageView android:id="@+id/fylke_vapen"
                   android:layout_width="wrap_content"
                   android:layout_height="match_parent"
                   android:layout_alignParentLeft="true"
                   android:scaleType="fitCenter"
                   android:src="@drawable/userpic_sidebar_default" />
    
        <TextView android:id="@+id/fylke_navn"
                  android:layout_width="wrap_content"
                  android:layout_height="match_parent"
                  android:layout_toRightOf="@id/fylke_vapen"
                  android:layout_toLeftOf="@id/star"
                  android:gravity="center_vertical"
                  android:paddingLeft="16dp"
                  android:textSize="24sp"
                  android:maxLines="1"
                  android:text="Placeholder text" />
    
        <CheckBox android:id="@+id/star"
                  android:layout_width="wrap_content"
                  android:layout_height="match_parent"
                  android:layout_alignParentRight="true"
                  android:button="@drawable/star_checkbox"
                  android:gravity="center"
                  android:focusable="false" />
    
    </RelativeLayout>
    

    【讨论】:

    • 删除这些属性会导致文本不在我的 2.3.3 和 4.2.2 上居中。
    • @vec 您可以使用LinearLayout 代替RelativeLayoutTextViewdrawableLeft 属性而不是TextViewImageView。试试这个方法,它只使用LinearLayout 中的两个视图。 http://pastebin.com/E7TeeCYZ
    【解决方案2】:

    好吧,我通过将 textview 包装在一个通过重力使 textview 居中的线性布局中解决了我的问题。

    似乎存在一个错误,您无法在 4.2.2 的列表视图中的 textview 上匹配_parent(我没有测试过 4.2.2 和 2.3.3 以外的任何其他版本)

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:padding="16dp" >
    
        <ImageView android:id="@+id/fylke_vapen"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:layout_alignParentLeft="true"
            android:scaleType="fitCenter"
            android:src="@drawable/fylke_buskerud" />
    
        <CheckBox android:id="@+id/star"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:layout_alignParentRight="true"
            android:button="@drawable/star_checkbox"
            android:focusable="false" /> 
    
        <LinearLayout 
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:layout_toRightOf="@id/fylke_vapen"
            android:layout_toLeftOf="@id/star"
            android:layout_alignBottom="@id/fylke_vapen"
            android:layout_alignTop="@id/fylke_vapen"
            android:paddingLeft="16dp"
            android:gravity="left|center" >
    
            <TextView android:id="@+id/fylke_navn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="24sp"
                android:maxLines="1"
                android:text="Placeholder text" />
        </LinearLayout>
    </RelativeLayout>
    

    【讨论】:

    【解决方案3】:
    // try this way,hope this will help you...
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/parent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:padding="10dp" >
    
        <ImageView
            android:id="@+id/fylke_vapen"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_launcher"/>
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center">
            <TextView
                android:id="@+id/fylke_navn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="24sp"
                android:maxLines="1"
                android:gravity="center"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:text="Placeholder text" />
        </LinearLayout>
    
    
        <CheckBox
            android:id="@+id/star"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/ic_launcher"
            android:focusable="false" />
    
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 2013-07-05
      • 1970-01-01
      • 2015-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多