2017-12-03 6 views
0

私はRecyclerViewに、私が中心にしたい単一のテキストビューを表示しています。RelativeLayoutとLinearLayoutを使用したリストのテキストビューの中央揃え

My活動レイアウト:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    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"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recyclerview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</LinearLayout> 

マイrecyclerview行レイアウト:

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

    <TextView 
     android:id="@+id/my_textview" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" /> 
</RelativeLayout> 

活動幅が画面の幅を埋める、recyclerviewはその幅と一致し、行RelativeLayoutはrecyclerview幅と一致しますtextviewはRelativeLayoutの幅に一致します。指定された重力は、テキストビューのコンテンツを水平方向にセンタリングすることを示します。したがって、テキストは画面の幅の中央に配置されます。

なぜこのロジックはLinearLayoutでは機能しませんか?行レイアウトのルートをLinearLayoutに変更し、重力属性が認識されないようにします。

答えて

1

android:layout_gravity="center"を使用すると、textViewを他のすべてに関連付けることができます。 android:gravity="center"はサブビューをセンターします。詳細については、hereを参照してください。

+0

テキストビューの幅が画面に表示されるため、これは機能しません。重力は「サブビュー」を中心に置かず、コンテンツを中心にします。この場合、テキストビューの重力はテキストの内容です。コンテンツに影響を与える重力のビューグループがある場合、これはサブビューになります。 – Marc

関連する問題