2011-08-26 88 views
6

我是android新手。如何以編程方式更改框架佈局高度和寬度?

我面對的一個問題是我加入的框架佈局,以我的應用程序代碼是

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <FrameLayout android:id="@+id/frameLayout1"> 
     <LinearLayout 
     android:layout_height="wrap_content" 
     android:id="@+id/linearLayout1" 
     android:layout_width="fill_parent"> 
     </LinearLayout> 
    </FrameLayout> 
</LinearLayout> 

我的問題是我想改變框架佈局的高度在不同的屏幕方向

示例代碼是

wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE); 

      getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 

      Log.v("height","height"+wm.getDefaultDisplay().getHeight()); 

      if(wm.getDefaultDisplay().getHeight() <= 427) 
      { 
       Log.v("height","111"); 


((FrameLayout)findViewById(R.id.frameLayout1)).setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); 
     } 
     else 
     { 
      ((FrameLayout)findViewById(R.id.frameLayout1)).setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); 
     } 

但它給部隊密切,

如果有任何人有想法,請幫助我。

在此先感謝。

回答

0

一種方法是構建自定義視圖並覆蓋onLayout和onMeasure函數。在onLayout函數中,您可以覆蓋寬度和高度。請參閱示例LabelView.java類here

3

你必須使用LinearLayout.LayoutParams(INT寬度,高度INT,浮動重量)

FrameLayout frameLayout = new FrameLayout(context); 
LinearLayout.LayoutParams layPra = new LinearLayout.LayoutParams(width,height,mWeight); 
layPra .gravity = Gravity.CENTER | Gravity.BOTTOM; 
frameLayout .setLayoutParams(layPra); 
相關問題