2012-12-05 65 views
11

在我的代碼中,我有一個XML文件有多個子佈局。 Aand我只想根據設備的屏幕寬度以編程方式設置其中一個子佈局的寬度。 直到現在我已經嘗試了佈局參數,setparam和其他方法,但它顯示空指針異常獲取佈局ID。以編程方式設置子版面寬度在ANDROID中?

我的父母佈局是一個相對佈局, 和第一個孩子佈局是一種線性佈局(「此佈局我需要改變寬度」)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/parentlayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout 
     android:id="@+id/childlayout" 
     android:layout_width="250dip" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" 
     android:background="#153746"> 
     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:background="@drawable/sub_title" 
      android:gravity="center_vertical" > 
      <TextView 
       android:id="@+id/menu_header_label" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textStyle="bold" 
       android:text="asd" 
       android:textColor="#ffffff" 
       android:layout_marginLeft="10dip" /> 

      <ImageView 
       android:id="@+id/menu_header" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal" /> 
     </LinearLayout> 
     <ListView 
      android:id="@+id/menu_listview" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:divider="@android:color/darker_gray" 
      android:dividerHeight="1dip" 
      android:scrollingCache="false" /> 
    </LinearLayout> 

    <FrameLayout 
     android:id="@+id/overlay" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 
    </FrameLayout> 

</LinearLayout> 

Java代碼: -

 public class SlideMenu extends LinearLayout { 

    // keys for saving/restoring instance state 
    public final static String KEY_MENUSHOWN = "menuWasShown"; 
    private final static String KEY_STATUSBARHEIGHT = "statusBarHeight"; 
    private final static String KEY_SUPERSTATE = "superState"; 

// Display display = ((WindowManager) getWindowToken()).getDefaultDisplay(); 
// int width = display.getWidth(); // deprecated 
// int height = display.getHeight(); // deprecated 
// float layWidth = (width*70)/100; 
// 
//// LinearLayout view= (LinearLayout)findViewById(R.id.childlayout); 
//// LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams(); 
//// params.width = 130; 
//// view.setLayoutParams(params); 
// view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, theSizeIWant)); 

    public static class SlideMenuItem { 
     public int id; 
     public Drawable icon; 
     public String label; 
    } 

這裏是我的代碼片段,我評論我到目前爲止所嘗試的部分,並得到任何好的結果..''

+0

哪裏是你的代碼? –

+0

不要將您的XML作爲屏幕截圖發佈,請將實際代碼放在問題中。 – Cerbrus

+0

這是在這裏回答http://stackoverflow.com/questions/5715612/how-to-set-setlayoutparams-for-linear-layout-elements –

回答

12

你能做到這一點:設置ID爲這個子線性佈局然後:

LinearLayout linLayout = (LinearLayout) findViewById(R.id.yourID); 
LinearLayout .LayoutParams layoutParams= new 
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
LinearLayout.LayoutParams.MATCH_PARENT);//or whatever , you can set the width and height programmatically and then setLayoutParams() 

編輯:

我看你已經嘗試過這個,對不起。但是有兩種線性佈局沒有?

+0

yup buddy有超過兩個線性佈局!!螺母thnx爲您的迴應.. :) – Manish

+0

有時我們看不到明顯的:) – vodich

6

把這個在java代碼:

int width = 300; 
int height = LayoutParams.WRAP_CONTENT; 
LinearLayout childLayout = (LinearLayout) findViewById(R.id.childLayout); 
childLayout.setLayoutParams(new LayoutParams(width, height)); 
+0

@ shreya:thnx這樣一個快速反應......但我也試過,它給予空指針異常在第四行,我們設置佈局參數:(. – Manish

+0

然後我認爲你還沒有找到正確的佈局引用。childLayout仍然是空的那一刻。請張貼您的代碼 –

+0

@ sherya-我剛剛更新了我的帖子!!請chech那。 – Manish

相關問題