2011-04-06 3 views
1

화면 너비의 절반으로 LinearLayout의 너비를 설정하고 내 UI 초기화에을 동적으로 설정하고 싶습니다.런타임 중에 Android 레이아웃 객체의 크기를 조정하는 방법은 무엇입니까?

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

     <LinearLayout 
      android:id="@+id/left_linear_layout" 
      android:layout_alignParentLeft="true" 
      android:layout_height="fill_parent" 
      android:layout_width="155dp" <!--want to set this to 1/2 screen width--> 
      android:orientation="vertical"> 
      ... 
     </LinearLayout> 
     <LinearLayout 
      android:id="@+id/right_linear_layout" 
      android:layout_alignParentRight="true" 
      android:layout_height="fill_parent" 
      android:layout_width="385dp"><!--want to set this relative to screen width as well--> 
      .... 
     </LinearLayout> 
</RelativeLayout> 

,이 문제가 해결 될 수 View 대신 Layout를 사용 : 나는 계층 구조는 다음과 같다하는 RelativeLayoutLinearLayout 감싸 있나요? 어떤 제안이라도 감사드립니다!

답변

0

layout_weight을 사용하여이를 수행 할 수 있지만 패딩에 보이지 않는보기를 추가해야합니다. 각 뷰가 소요됩니다

<RelativeLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
     <LinearLayout 
        android:id="@+id/left_linear_layout" 
        android:layout_alignParentLeft="true" 
        android:layout_height="fill_parent" 
        android:layout_width="0dp" 
        android:layout_weight="1" 
        android:orientation="vertical" 
        > 
      ... 
     </LinearLayout> 
     <!-- need this view to fill the other half of the screen --> 
     <View 
        android:id="@+id/spacer" 
        android:layout_toRightOf="@id/left_linear_layout" 
        android:layout_height="fill_parent" 
        android:layout_width="0dp" 
        android:layout_weight="1" 
        /> 

    .... 
</RelativeLayout> 

양은 layout_weight/total_layout_weight입니다 : 예를 들어 다음은 상단 패널을 절반 화면 폭을 만들 것입니다. 이 경우 total_layout_weight = 1+1 = 2이고 각보기는 layout_weight이 1이므로 각보기는 화면의 1/2을 차지합니다.

0

최상위 레이아웃으로 LinearLayout을 사용하고 두 개의 하위 레이아웃의 무게를 설정할 수 있습니다.

관련 문제