【问题标题】:Center a TextView inside another View in Android Relative Layout在 Android 相对布局中将 TextView 居中在另一个视图中
【发布时间】:2016-07-28 06:00:16
【问题描述】:

在我的相对布局中,我有一个圆形视图和一个 TextView。我想将 TextView 完全集中在圆形视图中。我怎么做?我到目前为止的代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <View
        android:id="@+id/firstCircle"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="24dp"
        android:background="@drawable/circle" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:id="@+id/number1" />

</RelativeLayout>

我希望最终结果看起来像:

Screenshot

【问题讨论】:

  • 在视图中获取文本视图
  • android:gravity="center"
  • android:layout_centerHorizo​​ntal="true" 在 TextView 中
  • 您能否分享一张您正在寻找的参考图片?
  • 视图不是 ViewGroup。意味着您不能在其中使用 TextView 或 Button。 View 和 ViewGroup 之间存在很大差异。参考这个stackoverflow.com/a/34676816/6176457@KrishnaJ

标签: android android-layout textview


【解决方案1】:

试试这个

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

    <View
        android:id="@+id/firstCircle"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="24dp"
        android:background="@drawable/circle" >
    </View>

    <TextView
        android:id="@+id/number1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="28dp"
        android:text="1" />

</RelativeLayout>

【讨论】:

    【解决方案2】:

    将视图粘贴到其他 framelyout 上。

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <View
                android:id="@+id/firstCircle"
                android:layout_width="24dp"
                android:layout_height="24dp"
                android:layout_marginLeft="24dp"
                android:layout_marginTop="24dp"
                android:background="@drawable/circle" />
    
            <TextView
                android:id="@+id/number1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1"
                android:layout_alignBottom="@+id/firstCircle"
                android:layout_alignRight="@+id/firstCircle"
                android:layout_alignEnd="@+id/firstCircle"
                android:layout_alignLeft="@+id/firstCircle"
                android:layout_alignStart="@+id/firstCircle"
                android:layout_alignTop="@+id/firstCircle"
                android:gravity="center" />
        </RelativeLayout>
    
    </FrameLayout>
    

    【讨论】:

      【解决方案3】:
      <?xml version="1.0" encoding="utf-8"?>
      
      
      <RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          xmlns:android="http://schemas.android.com/apk/res/android">
      
              <View
                  android:id="@+id/firstCircle"
                  android:layout_width="24dp"
                  android:layout_height="24dp"
                  android:layout_marginLeft="24dp"
                  android:layout_marginTop="24dp"
                  android:background="@drawable/circle" />
      
              <TextView
                  android:id="@+id/number1"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="1"
                  android:layout_alignBottom="@+id/firstCircle"
                  android:layout_alignRight="@+id/firstCircle"
                  android:layout_alignEnd="@+id/firstCircle"
                  android:layout_alignLeft="@+id/firstCircle"
                  android:layout_alignStart="@+id/firstCircle"
                  android:layout_alignTop="@+id/firstCircle"
                  android:gravity="center" />
          </RelativeLayout>
      

      【讨论】:

      • 在您的文本视图中,只需在上面的代码中将 textsize 设置为 7-9 sp
      【解决方案4】:

      你不需要使用一些额外的View,只需将圆形图像设置为TextView的背景并将gravity设置为center如下:

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              xmlns:app="http://schemas.android.com/apk/res-auto">
      
      
              <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="1"
                  android:gravity="center"
                  android:background="@drawable/circle"
                  android:id="@+id/number1" />
      
          </RelativeLayout>
      

      【讨论】:

        【解决方案5】:

        通过以下任何一种布局更改您的视图会更好,

         <LinearLayout
            android:id="@+id/firstCircle"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_marginLeft="24dp"
            android:layout_marginTop="24dp"
            android:gravity="center"
            android:layout_gravity="center"
            android:background="@drawable/circle">
        
           <TextView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="1"
               android:gravity="center"
               android:id="@+id/number1" />
        </LinearLayout>
        

        【讨论】:

        • 他要的是一个非线性的相对的;)
        • 我知道并且我说过要使用另一个内部视图作为线性或相对任何以获得更好的性能
        【解决方案6】:

        我想你希望 circleview 作为 TextView 的背景。那么为什么不将视图作为 TextView 的背景。

        试试这个。

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            xmlns:app="http://schemas.android.com/apk/res-auto">
        
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/circle"
                android:text="1"
                android:gravity="center"
                android:padding="10dp"
                android:id="@+id/number1" />
        
        </RelativeLayout>
        

        注意:您可以根据需要调整填充

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-02-04
          • 1970-01-01
          • 1970-01-01
          • 2013-01-29
          • 2012-04-10
          • 2016-11-25
          • 2016-07-05
          • 2011-02-16
          相关资源
          最近更新 更多