2016-07-28 100 views
-1

在我的相对布局中,我有一个圆形视图和一个TextView。我想将TextView精确地置于圆形视图中。我怎么做?我到目前为止的代码如下:在Android的另一个视图中居中放置一个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

+0

让Textview在视图内 – KrishnaJ

+0

android:gravity =“center” – Rohit

+0

android:layout_centerHorizo​​ntal =“true”在TextView中 –

回答

0

你并不需要使用一些额外的View,只需设置圈形象的TextView背景和设置如下gravitycenter

<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> 
0

任何一个布局更改您的视图,它下面会更好地工作,

<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> 
+0

他是要求一个相对不线性;) –

+0

我知道,我说使用另一个内部视图作为线性或相对任何更好的性能 – Vickyexpert

0

我想你想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> 

注:,你可以根据你的需要调整填充

0

使用其他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> 
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> 
0
<?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> 
+0

在您的textview只是设置文本大小说7-9 sp在上面的代码 –

0
<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" 
     android:layout_centerInParent="true"/> 

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

</RelativeLayout> 

您可以根据您的需要调整圈子视图的填充。

相关问题