2014-01-20 3 views
11

다음과 같은 레이아웃이 있습니다. 이제 상대 레이아웃의 너비를 고정 240 dp로 설정하고 싶지 않습니다. 상대 레이아웃의 너비를 화면 너비의 1/3로 설정하고 싶습니다. xml 파일에서 그렇게 할 수 있습니까? 그것이 불가능하다면 어떻게 자바 코드를 사용하여 그것을 달성 할 수 있습니까?상대 너비의 너비를 화면 너비의 1/3로 설정 하시겠습니까?

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@+id/fullscreen" 
     style="@style/translucent"> 
      <RelativeLayout 
      android:layout_width="240dp" 
      android:layout_height="fill_parent" 
      android:layout_gravity="right" 
      android:gravity="center" 
      android:background="#88000000" 
      android:id="@+id/sidebar"> 

      </RelativeLayout> 

    </FrameLayout> 

답변

20

부모가 weightsum="3"이고 자식이 layout_weight=1입니다. android:layout_orientation="horizontal"Take a look a this reference

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/fullscreen" 
    style="@style/translucent" 
    android:orientation="horizontal" 
    android:weightSum="3"> 

    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_gravity="right" 
     android:gravity="center" 
     android:background="#88000000" 
     android:id="@+id/sidebar" 
     android:layout_weight="1" 
     > 

    </RelativeLayout> 

    <!-- other views, with a total layout_weight of 2 --> 

</LinearLayout> 
+0

LinearLayout에 relativelayout이 하나 뿐인 경우는 무엇입니까? relativelayout에서 layout_weight = 1 만 설정할 수 있습니까? –

+0

시도해 볼 수 있으며 직접 배우실 수 있습니다! <: layout_width = "0dp" 안드로이드 : layout_height = "match_parent" 안드로이드 :에 layout_weight = "2"보기 안드로이드> :-) 어쨌든, 당신은 항상 '뭔가를이 목적을 위해 더미보기를 사용할 수있는 기억'는 아무것도 표시하지 않고 여분의 공간을 사용합니다 –

2

당신은 parentview의 세 번째로 뷰의 폭을 얻기 위해 LinearLayout을 사용해야합니다. 같은

뭔가 :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:background="#88000000"> 
    </RelativeLayout> 
    <ViewGroup 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="2"> 
    </ViewGroup> 
</LinearLayout> 

핵심 비트는 layout_weights의 비율이다. documentation은 꽤 좋습니다.

1

LinearLayout는 무게와 함께 원하는 것입니다.

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal"> 

    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     ...all your stuff... /> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="2" /> 


</LinearLayout> 
관련 문제