Android relative layout problem with gravity

后端 未结 5 749
生来不讨喜
生来不讨喜 2021-01-01 09:32

How can i put textview with id=\"naslov\" to the center? I also tried with layout_gravity=\"center\" but that doesn\'t work either.



        
相关标签:
5条回答
  • 2021-01-01 09:57
     android:layout_centerHorizontal="true"
    

    there is also

    android:layout_centerInParent="true"
    

    Full list for RelativeLayout attributes is here

    Also you can tell your TextView to fill_parent and then set gravity=center on it. So it'll center actual text within textView.

    0 讨论(0)
  • 2021-01-01 09:59

    You can go to the graphical layout tab in the activity_main.XML and select you item you want to position and change it from the three tabs on the top. Or you can just put this: android:layout_gravity="center_horizontal".

    0 讨论(0)
  • 2021-01-01 10:06

    Relative Layout does not make use of layout_gravity, however you use gravity values in following ways : center, left , left|center , right|center , right, top, bottom.

    <!-- below widget displaying text in left center -->
    <TextView
               android:id="@+id/song_title"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:text="sample name text"
               android:gravity="left|center"
               android:textSize="15sp" />
    
    0 讨论(0)
  • 2021-01-01 10:07
     android:layout_centerHorizontal="true"
    
    0 讨论(0)
  • 2021-01-01 10:22

    Supplemental Answer

    As a more general answer to this question, layout_gravity does not work with the subviews of a RelativeLayout. It is for use with a LinearLayout or FrameLayout. However, the subviews of a RelativeLayout can still use gravity as usual because this is just how a view arranges its own content.

    See the comparison in the following image. The green and blue views are TextViews inside of a RelativeLayout.

    The gravity works but layout_gravity doesn't. See my fuller answer for more details.

    See also

    • Gravity and layout_gravity on Android
    • Positioning views within a RelativeLayout
    0 讨论(0)
提交回复
热议问题