【问题标题】:Centering Textview Below a image button that is under relative layout?在相对布局下的图像按钮下方居中 Textview?
【发布时间】:2012-07-13 07:33:12
【问题描述】:

我有这个文本视图没有在图像按钮下方居中的问题。无论如何要解决这个问题?编码和图像在下面给出!

   <LinearLayout android:gravity="center_vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp"> 

       <RelativeLayout  android:gravity="center"  android:layout_weight="1"  android:layout_width="wrap_content" android:layout_height="wrap_content"> 

  <ImageButton
      android:id="@+id/imageButton1"
      android:layout_width="100dp"
      android:layout_height="100dp"
      android:background="@null"
      android:onClick="btnCat1"
      android:src="@drawable/nopic" >
</ImageButton>

<TextView
    style="@style/MenuStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imageButton1"
    android:layout_centerInParent="true"
    android:clickable="false"
    android:text="text" />

</RelativeLayout>

       <RelativeLayout  android:gravity="center"  android:layout_weight="1"  android:layout_width="wrap_content" android:layout_height="wrap_content"> 

  <ImageButton
      android:id="@+id/imageButton1"
      android:layout_width="100dp"
      android:layout_height="100dp"
      android:background="@null"
      android:onClick="btnContact"
      android:src="@drawable/nopic" >
</ImageButton>

<TextView
    style="@style/MenuStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imageButton1"
    android:layout_centerInParent="true"
    android:clickable="false"
    android:text="text" />

</RelativeLayout>
</LinearLayout>






   <LinearLayout android:gravity="center_vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp"> 

       <RelativeLayout  android:gravity="center"  android:layout_weight="1"  android:layout_width="wrap_content" android:layout_height="wrap_content"> 

  <ImageButton
      android:id="@+id/imageButton1"
      android:layout_width="100dp"
      android:layout_height="100dp"
      android:background="@null"
      android:onClick="btnRoute"
      android:src="@drawable/nopic" >
</ImageButton>

<TextView
    style="@style/MenuStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imageButton1"
    android:layout_centerInParent="true"
    android:clickable="false"
    android:text="text" />

</RelativeLayout>

       <RelativeLayout  android:gravity="center"  android:layout_weight="1"  android:layout_width="wrap_content" android:layout_height="wrap_content"> 

  <ImageButton
      android:id="@+id/imageButton1"
      android:layout_width="100dp"
      android:layout_height="100dp"
      android:background="@null"
      android:onClick="btnChecList"
      android:src="@drawable/nopic" >
</ImageButton>

<TextView
    style="@style/MenuStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/imageButton1"
    android:layout_centerInParent="true"
    android:clickable="false"
    android:text="text" />

</RelativeLayout>
</LinearLayout>




</LinearLayout>

【问题讨论】:

  • 如果给出拒绝投票的理由会很棒:(

标签: android android-layout


【解决方案1】:

试试这个 sn-p:

<RelativeLayout 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" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:gravity="center_vertical" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@null"
            android:onClick="btnCat1"
            android:src="@drawable/ic_launcher" >
        </ImageButton>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageButton1"
            android:layout_centerInParent="true"
            android:clickable="false"
            android:text="text" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@null"
            android:onClick="btnCat1"
            android:src="@drawable/ic_launcher" >
        </ImageButton>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageButton1"
            android:layout_centerInParent="true"
            android:clickable="false"
            android:text="text" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@null"
            android:onClick="btnCat1"
            android:src="@drawable/ic_launcher" >
        </ImageButton>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageButton1"
            android:layout_centerInParent="true"
            android:clickable="false"
            android:text="text" />
    </RelativeLayout>
</LinearLayout>

【讨论】:

    【解决方案2】:

    你可以这样做,简单地(!!!):

    android:layout_alignRight="@id/my_image_view"
    android:layout_alignLeft="@id/my_image_view"
    

    只要左右对齐,结果就是TextView居中。

    【讨论】:

    • 使用这种方法,文本的宽度将被限制为图标的宽度。如果图标与文本的长度相比较窄,则文本将换行数次。
    • 是的,解决方法是将 TextView 放在 ViewGroup(例如 FrameLayout)中,宽度和高度设置为 wrap_content,并将属性 clipChildren 和 clipToPadding 设置为 false。这个 ViewGroup 将在图像下方居中,其中的 TextView 将完全可见。
    • 这似乎不起作用 - 在 Android Studio 的设计视图和 5.0.x 设备上进行了测试。我有一个 FrameLayout,设置为包装其内容,两个剪辑选项都设置为 false。 FrameLayout 中的 TextView 仍然限制在其下方居中图标的宽度。
    • 根据我的经验,为了让clipchildren按您的预期工作,您还必须将该属性设置为textview的祖父母...stackoverflow.com/a/26528854/1507512
    【解决方案3】:

    只需添加这些值:

     <TextView
        android:layout_alignLeft="@+id/my_image_view"
        android:layout_alignRight="@+id/my_image_view"
        android:gravity="center" />
    

    【讨论】:

      【解决方案4】:

      将按钮和文本视图放在同一垂直的LinearLayout 中(您可以将LinearLayout 放置在相对布局中)并简单地将中心重心分配给文本视图。

      【讨论】:

        【解决方案5】:

        android:layout_centerHorizontal="true"添加这个

        【讨论】:

          【解决方案6】:

          从文本视图中删除 android:layout_centerInParent="true"

          或者试试这个

          <LinearLayout  android:gravity="center"  android:layout_weight="1"  android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> 
          
                  <ImageButton
                      android:contentDescription="@string/app_name"
                      android:id="@+id/imageButton1"
                      android:layout_width="100dp"
                      android:layout_height="100dp"
                      android:background="@null"
                      android:onClick="btnCat1"
                      android:src="@drawable/ic_launcher" >
                  </ImageButton>
          
                  <TextView
                      style="@style/MenuStyle"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:clickable="false"
                      android:text="@string/app_name" 
                      android:textColor="@color/black"/>
          
                  </LinearLayout>
          

          【讨论】:

            【解决方案7】:

            顺便说一句,我尝试以相反的方式进行操作。我从水平变为垂直并且它工作了

            <?xml version="1.0" encoding="utf-8"?>
            <LinearLayout
            android:id="@+id/widget33"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:background="#FFFFFF" >
            
            
               <LinearLayout  android:gravity="center" android:layout_weight="1" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp"> 
            
                   <RelativeLayout  android:gravity="center"   android:layout_weight="1"  android:layout_width="wrap_content" android:layout_height="wrap_content"> 
            
                <ImageButton
                  android:id="@+id/imageButton1"
                  android:layout_width="100dp"
                  android:layout_height="100dp"
                  android:background="@null"
                  android:onClick="btnCat1"
                  android:src="@drawable/nopic" >
            </ImageButton>
            
            <TextView
                style="@style/MenuStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/imageButton1"
                android:layout_centerHorizontal="true"
                android:clickable="false"
                android:text="Cat 1" />
            
            
            </RelativeLayout>
            
                   <RelativeLayout  android:gravity="center"  android:layout_weight="1"  android:layout_width="wrap_content" android:layout_height="wrap_content"> 
            
              <ImageButton
                  android:id="@+id/imageButton1"
                  android:layout_width="100dp"
                  android:layout_height="100dp"
                  android:background="@null"
                  android:onClick="btnRoute"
                  android:src="@drawable/nopic" >
            </ImageButton>
            
            <TextView
                style="@style/MenuStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/imageButton1"
                android:layout_centerHorizontal="true"
                android:clickable="false"
                android:text="Route" />
            
            </RelativeLayout>
            </LinearLayout>
            
            
            
               <LinearLayout  android:gravity="center" android:layout_weight="1" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp"> 
            
                   <RelativeLayout  android:gravity="center"   android:layout_weight="1"  android:layout_width="wrap_content" android:layout_height="wrap_content"> 
            
                <ImageButton
                  android:id="@+id/imageButton1"
                  android:layout_width="100dp"
                  android:layout_height="100dp"
                  android:background="@null"
                  android:onClick="btnContact"
                  android:src="@drawable/nopic" >
            </ImageButton>
            
            <TextView
                style="@style/MenuStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/imageButton1"
                android:layout_centerHorizontal="true"
                android:clickable="false"
                android:text="Contacts" />
            
            
            </RelativeLayout>
            
                   <RelativeLayout  android:gravity="center"  android:layout_weight="1"  android:layout_width="wrap_content" android:layout_height="wrap_content"> 
            
            
              <ImageButton
                  android:id="@+id/imageButton1"
                  android:layout_width="100dp"
                  android:layout_height="100dp"
                  android:background="@null"
                  android:onClick="btnChecList"
                  android:src="@drawable/nopic" >
            </ImageButton>
            
            <TextView
                style="@style/MenuStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/imageButton1"
                android:layout_centerHorizontal="true"
                android:clickable="false"
                android:text="CheckList" />
            
            </RelativeLayout>
            </LinearLayout>
            
            
            
            
            
            
            
            </LinearLayout>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-02-04
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-01-18
              相关资源
              最近更新 更多