4
목록보기의 행

내 XML 코드가는 : layout_gravity = "오른쪽"안드로이드 XML 여기에 작동하지

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:baselineAligned="false" 
    android:orientation="horizontal" > 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="5" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/tv_name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" /> 

     <TextView 
      android:id="@+id/tv_time" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_weight="2" 
     android:gravity="center" > 

     <ImageView 
      android:id="@+id/iv_image" 
      android:layout_width="50dp" 
      android:layout_height="40dp" 
      android:background="@drawable/app_icon_17" 
      android:contentDescription="@string/empty" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:layout_weight="2" 
     android:gravity="center" > 

     <Button 
      android:id="@+id/btn_delete" 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:background="@drawable/btn_delete_new" 
      android:focusable="false" /> 
    </LinearLayout> 

</LinearLayout> 

이다 나는 오른쪽 정렬 머물 아이디 btn_delete와 마지막 단추를 원한다. 그리고 디자인하면서 "그래픽 레이아웃"에서 원하는대로 보여줍니다. 하지만 에뮬레이터를 실행하면 작동하지 않습니다.

enter image description here

이 왜 삭제 단추를 잘 작성하지 못했습니다 정렬하기 :

출력을 참조하십시오?

+2

내가 생각하는'RelativeLayout'는 나'FrameLayout' 다음 모두 다음 더 나은'LinearLayout' 당신은 당신이 원하는 것을 얻을 것이다 .. 나는 목록 행 이러한 유형의 디자인 등'RelativeLayout'을 선호했다. – user370305

+1

버튼에 layout_gravity를 추가하고 사용해보세요! – Gridtestmail

+9

이기 때문에 수평 선형 레이아웃은 layout_gravity 오른쪽, 왼쪽 및 수평 중심을 무시하기 때문입니다. 수직 중력에만주의를 기울입니다. – njzk2

답변

6

사용을 사용합니다.

부모 레이아웃에 무게를 할당하면 높이 또는 0dp을 설정하면됩니다. 당신이 가중 레이아웃의 아이의 레이아웃이있을 때 귀하의 경우에는 는 android:layout_width="0dp"

하지만 당신은 그 재산을 레이아웃 제공해야합니다 fill_parent 예 : 버튼의 android:layout_height="fill_parent"

그래서 당신은 당신의 부모 레이아웃을 변경하지 않는 경우 기존 코드로 작업하려면 코드 아래에서 시도하십시오. 당신이 사용하는 경우

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:baselineAligned="false" 
    android:orientation="horizontal" > 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="5" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/tv_name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" /> 

     <TextView 
      android:id="@+id/tv_time" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="2" 
     > 

     <ImageView 
      android:id="@+id/iv_image" 
      android:layout_width="fill_parent" 
      android:layout_height="40dp" 
      android:background="@drawable/icon" 
      android:contentDescription="@string/app_name" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="2" > 

     <Button 
      android:id="@+id/btn_delete" 
      android:layout_width="fill_parent" 
      android:layout_height="40dp" 
      android:background="@drawable/icon" 
      android:focusable="false" /> 

    </LinearLayout> 

</LinearLayout> 
-3
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="2" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/btn_delete" 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:layout_gravity="right" 
     android:gravity="right" 
     android:background="@drawable/btn_delete_new" 
     android:focusable="false" /> 
</LinearLayout> 
+1

이 답변은 무엇을 나타 냅니까? –

+1

설명이없는 코드는 거의 도움이되지 않습니다. – NoChinDeluxe

0

변경이

<LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:layout_weight="2" 
     android:gravity="center" > 

     <Button 
      android:id="@+id/btn_delete" 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:background="@drawable/btn_delete_new" 
      android:focusable="false" /> 
    </LinearLayout> 

<LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     <Button 
      android:id="@+id/btn_delete" 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:layout_gravity="right" 
      android:background="@drawable/btn_delete_new" 
      android:focusable="false" /> 
    </LinearLayout> 

에 당신은 레이아웃에 android:layout_gravity="right"을 적용했다. 대신 레이아웃 내부의 버튼에 적용해야합니다. 그리고 나는 그 레이아웃에 무게 2가 필요하다고 생각하지 않습니다. 그러나 필요한 경우 추가 할 수 있습니다. 희망이 도움이됩니다.

+0

감사합니다 @Anamika –

+0

@ ShirishHerwade 환영합니다. :) –

5

오해 한 것 같은데, LinearLayouts을 포함하는 것은 필요하지 않습니다. 우리는 당신이 당신의 행 레이아웃에 가중치를 부여 있다고 볼 수 있듯이 상대 레이아웃

<?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="match_parent" > 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" > 

     <TextView 
      android:id="@+id/tv_name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="10dp" /> 

     <TextView 
      android:id="@+id/tv_time" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/tv_name" 
      android:layout_marginLeft="10dp" /> 
    </RelativeLayout> 

    <RelativeLayout 
     android:id="@+id/rl_btn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" > 

     <Button 
      android:id="@+id/btn_delete" 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:background="@drawable/btn_delete_new" 
      android:focusable="false" /> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="20dp" 
     android:layout_toLeftOf="@+id/rl_btn" > 

     <ImageView 
      android:id="@+id/iv_image" 
      android:layout_width="50dp" 
      android:layout_height="40dp" 
      android:background="@drawable/app_icon_17" 
      android:contentDescription="@string/empty" /> 
    </RelativeLayout> 

</RelativeLayout> 
+0

나는 그들을 이해하기 위해 무엇을 읽을 수 있습니까? 그리고 무게도 이해해야합니다. – Anas

1

를 사용하여 RelativeLayout, 예컨대 :

<?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/tv_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="10dp" /> 

    <TextView 
     android:id="@+id/tv_time" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="10dp" /> 

    <ImageView 
     android:id="@+id/iv_image" 
     android:layout_width="50dp" 
     android:layout_height="40dp" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="10dp" 
     android:layout_toRightOf="@id/tv_time" 
     android:background="@drawable/app_icon_17" 
     android:contentDescription="@string/empty" /> 

    <Button 
     android:id="@+id/btn_delete" 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:layout_centerVertical="true" 
     android:layout_alignParentRight="true" 
     android:background="@drawable/btn_delete_new" 
     android:focusable="false" /> 

</RelativeLayout>