2010-09-26 58 views
0

我正面临自定义线性布局的问题。我的XML如下:LinearLayout问题

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#ffffff" 
    > 

    <LinearLayout android:id="@+id/LinearLayout_LeftPanel" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:background="#0000ff"  
     > 
     <TextView android:id="@+id/LeftPanel_Title" 
      android:text="Frequently asked questions" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#000000" 
      /> 

    </LinearLayout> 


    <LinearLayout android:id="@+id/LinearLayout_MainContent"   
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"   
     android:gravity="center" 
     android:layout_toRightOf="@+id/LinearLayout_LeftPanel"  
     > 
     <TextView android:id="@+id/MainContent_Title" 
      android:text="Alabama Rules of Civil Procedure" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#ff0000" 
      android:textSize="19px" 
      android:textStyle="bold" 
      />  
    </LinearLayout> 

</RelativeLayout> 

我要设置@ + ID/LinearLayout_LeftPanel布局宽度的屏幕宽度的三分之一亲语法上的onCreate()。 那么如何做到这一点?

回答

1

可以使用同时设置Linearlayouts

设定第一机器人的重量属性:layout_weight = “1” 和左侧面板机器人:layout_weight = “3”

感谢

2

Mina Samy的回答几乎是正确的,但是您将使用RelativeLayout作为最顶层的容器。 Android的:layout_weight属性,只有当周围的容器是一个的LinearLayout作品:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"> 

     <LinearLayout android:id="@+id/LeftPanel" 
        android:layout_weight="1"> 
      ... 
     </LinearLayout> 

     <LinearLayout android:id="@+id/MainContent" 
        android:layout_weight="3"> 
      ... 
     </LinearLayout> 

</LinearLayout> 

编辑: 我刚才看到你说你想设置“程序在onCreate方法”。如果你真的想这样做,而不是像我上面写的XML那样设置它,你必须这样做:

protected void onCreate(Bundle b) { 
    LinearLayout leftPanel = (LinearLayout)findViewById(R.id.LeftPanel); 
    LinearLayout.LayoutParams leftPanelParams = (LinearLayout.LayoutParams)leftPanel.getLayoutParams(); 
    leftPanelParams.weight = 1; 

    // do the same thing for MainContent but set the Weight to 3; 
}