Program Tip

LinearLayout을 부모 중심에 정렬하는 방법은 무엇입니까?

programtip 2020. 10. 22. 22:23
반응형

LinearLayout을 부모 중심에 정렬하는 방법은 무엇입니까?


나는 여기에서 많은 유사한 질문을 살펴 보았지만 아무것도 도움이되지 않습니다. 나는 다른 중첩 레이아웃의 계층 구조를 가지고 있으며 모두가 android:layout_width="fill_parent"있고 가장 안쪽 레이아웃에는 android:layout_width="wrap_content-중앙 (가로)에 정렬해야합니다. 어떻게하나요?

업데이트 : : 문제의 원인을 찾았습니다-내부 LinearLayout을 RelativeLayout android:layout_width="fill_parent"에 넣더라도 여전히 내용을 래핑합니다. 그러나 TableRow는 실제로 화면을 채우고 있습니다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
            <TableRow >
            <LinearLayout 
            android:orientation="horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                    <TextView
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:text="1"
                    android:textAppearance="?android:attr/textAppearanceLarge" />

                     <TextView
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:text="2"
                    android:textAppearance="?android:attr/textAppearanceLarge" />
             </LinearLayout>
           </TableRow>
        </TableLayout>
    </FrameLayout>
</LinearLayout>

이 두 속성은 일반적으로 혼동됩니다.

  • android:gravity 사용되는 뷰의 콘텐츠 중력을 설정합니다.
  • android:layout_gravity부모를 기준으로 뷰 또는 레이아웃의 중력을 설정합니다 .

따라서 android:gravity="center"부모 또는 android:layout_gravity="center"LinearLayout 자체에 두십시오 .

나는 여러 번 그들을 섞어서 왜 사물이 제대로 중앙에 있지 않은지 궁금해했습니다.


선형 레이아웃에서 이것을 시도하십시오

android:layout_centerHorizontal="true"
android:layout_centerVertical="true"

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    <LinearLayout 
            android:orientation="horizontal"
            android:layout_centerHorizontal="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <TextView
                 android:layout_height="wrap_content"
                 android:layout_width="wrap_content"
                 android:text="1"
                 android:textAppearance="?android:attr/textAppearanceLarge" />

           <TextView
                 android:layout_height="wrap_content"
                 android:layout_width="wrap_content"
                 android:text="2"
                 android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>
</RelativeLayout>

relativeLayout을 LinearLayout보다 래핑하는 것을 고려하십시오. android:layout_centerHorizontal="true"뷰 중심을 수평으로 배치합니다.


layout_gravity="center"또는 "center_horizontal"부모 레이아웃에 추가 합니다.

참고로, a 는 이미 수평이므로 LinearLayout내부 TableRow가 불필요하게 보입니다 .TableRowLinearLayout


이 시도:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    >
    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >
            <TableRow >
            <LinearLayout 
            android:orientation="horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal" 
                    android:layout_gravity="center_horizontal" >
                    <TextView
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:text="1"
                    android:textAppearance="?android:attr/textAppearanceLarge" />

                     <TextView
                    android:layout_height="wrap_content"
                    android:layout_width="wrap_content"
                    android:text="2"
                    android:textAppearance="?android:attr/textAppearanceLarge" />
             </LinearLayout>
           </TableRow>
        </TableLayout>
    </FrameLayout>
</LinearLayout>

이것은 나를 위해 일했습니다 .. 빈보기 추가 ..

<LinearLayout
       android:layout_width="match_parent"
        android:layout_height="wrap_content"
       android:layout_gravity="center_horizontal"
        android:orientation="horizontal"
         >
<View
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="1"
            />  
     <com.google.android.gms.ads.AdView

         android:id="@+id/adView"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"

         android:layout_gravity="center_horizontal"
         ads:adSize="BANNER"
         ads:adUnitId="@string/banner_ad_unit_id" >

    </com.google.android.gms.ads.AdView>
    <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="1"
            />  
    </LinearLayout>

Try <TableRow android:gravity="center_horizontal">This는 테이블 행 내에서 내부 LinearLayout을 중앙에 배치합니다.


사용자 지정 알림을 디자인하는 동안 동일한 상황에 직면했습니다. 다음 속성을 true로 설정했습니다.

 android:layout_centerInParent.

@jksschneider explation is almost right. Make sure that you haven't set any gravity to parent layout, and then set layout_gravity="center" to your view or layout.


this worked for me.

<LinearLayout>
.
.
.
android:gravity="center"
.
.>

<TextView
android:layout_gravity = "center"
/>
<Button
android:layout_gravity="center"
/>

</LinearLayout>

so you're designing the Linear Layout to place all its contents(TextView and Button) in its center and then the TextView and Button are placed relative to the center of the Linear Layout


I experienced this while working with nested RelativeLayouts. My solution ended up being simple, but it's a little gotcha that's worth being aware of.

My layout was defined with...

android:layout_height="fill_parent"
android:layout_below="@id/someLayout
android:gravity="center"

...however, I didn't think that the layout below this one was placed on top of it. Everything inside this layout was shifted towards the bottom instead of centered. Adding...

android:layout_above="@id/bottomLayout"

...fixed my issue.


for ImageView or others views who layout-gravity or gravity does not exist use : android:layout_centerInParent="true" in the child (I had a relative layout with an ImageView as a child).


The problem is that the root's (the main layout you store the other elements) gravity is not set. If you change it to center the other elements' gravity must work just fine.


Below is code to put your Linearlayout at bottom and put its content at center. You have to use RelativeLayout to set Layout at bottom.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="#97611F"
    android:gravity="center_horizontal"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:text="View Saved Searches"
        android:textSize="15dp"
        android:textColor="#fff"
        android:gravity="center_vertical"
        android:visibility="visible" />
    <TextView
        android:layout_width="30dp"
        android:layout_height="24dp"
        android:text="12"
        android:textSize="12dp"
        android:textColor="#fff"
        android:gravity="center_horizontal"
        android:padding="1dp"
        android:visibility="visible" />
  </LinearLayout>  
</RelativeLayout>

참고URL : https://stackoverflow.com/questions/8180887/how-to-align-linearlayout-at-the-center-of-its-parent

반응형