2012-12-05 4 views
11

내 코드에는 여러 자식 레이아웃이있는 XML 파일이 있습니다. Aand 그냥 장치의 화면 너비에 따라 이러한 자식 레이아웃의 너비 중 하나를 프로그래밍 방식으로 설정하려고합니다. 지금까지 레이아웃 매개 변수, setparam 및 기타 메서드를 시도했지만 레이아웃 ID를 가져 오는 데 null 포인터 예외가 표시됩니다.자식 레이아웃 너비를 프로그래밍 방식으로 설정 ANDROID?

내 부모 레이아웃은 상대 레이아웃, 및 제 1 회 아이의 레이아웃은 선형 레이아웃 ("이 레이아웃에 내가 폭 변경해야")

<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> 

자바 코드입니다 : -

 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

옙 친구 이상이 있습니다 2 개의 선형 레이아웃 !! 너의 응답을 위해 너트 thnx : – Manish

+0

가끔은 우리가 분명히 볼 수 없다 :) – vodich

6

넣고이 :

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

@ shreya : 빠른 응답을위한 thnx ...하지만 또한 그것을 시도, 그것은 4 라인에서 널 포인터 예외를 제공 어디 우리는 레이아웃 파라미터를 설정하십시오 :(. – Manish

+0

그럼 레이아웃 참조를 올바르게 찾지 못했을 것입니다. 그 시점에서 childLayout은 여전히 ​​null입니다. –

+0

@ sherya- 방금 내 게시물을 업데이트했습니다. chech that please. – Manish

관련 문제