2017-02-11 48 views
2

我无法将我的textview放置在RelativeLayout中间。我添加了这个参数在RelativeLayout中水平居中TextView不起作用

android:layout_centerHorizontal="true" 
android:layout_centerInParent="true" 

但它没有帮助。

我的布局

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/content_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="?android:attr/colorBackground" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="ru.myseolife.a99torrentsalpha2.MainActivity" 
    android:gravity="center" 
    tools:showIn="@layout/app_bar_main" 
    > 

    <TextView 
     android:id="@+id/nothing_found" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerInParent="true" 
     android:text="@string/nothingfound" 
     /> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recyclerView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"/> 

    <LinearLayout 
     android:id="@+id/linlaHeaderProgress" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center" 
     android:orientation="vertical" 
     android:visibility="gone" 
     > 

     <ProgressBar 
      android:id="@+id/progressBar" 
      style="@style/Widget.AppCompat.ProgressBar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      /> 
    </LinearLayout> 
</RelativeLayout> 

有人知道是否可以? TextView垂直居中,但水平显示在左侧。

+0

将android:gravity =“center”添加到您的textview。 –

+0

@ AbdenaceurLichiheb谢谢,它帮助,但我认为android:gravity =“center”适用于子元素。 –

+0

在textview的情况下,它将应用于文本,它是否工作? –

回答

1

在你TextViewlayout_widthmatch_parent如果您希望保留并显示为中心的,你需要居中文本里面看不到TextView

所以用android:gravity="center"在你的TextView里面或者你可以用TextView的宽度wrap_content并保持原样。

因为它是一个简单的答案,我会告诉你一招>去 设置>开发人员选项>并勾选显示布局边界,那么你可以检查视图边界,让这里居然视图边界的想法!

+0

很棒的工作,很好的解释。 –

+1

@ Abdenaceur Lichiheb那么为什么我应该复制粘贴某人评论0.13%在这个季度?你不是唯一知道android先生的人! –

0

的TextView的宽度应该是“WRAP_CONTENT”

0

补充一点:

android:gravity="center_horizontal" 
0

添加

android:gravity="center" 

您TextView的,因为你设置宽度匹配父这将居中文本。