2010-09-26 41 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; 
}