如何以编程方式更改框架布局的高度和宽度?

kir*_*ran 6 android-layout

我是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>
Run Code Online (Sandbox Code Playgroud)

我的问题是我想在不同的屏幕方向上更改框架布局高度

示例代码是

    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));
        }
Run Code Online (Sandbox Code Playgroud)

但它让力量接近,

如果有任何想法请帮助我.

提前致谢.

tjb*_*tjb 0

一种方法是构建自定义视图并覆盖 onLayout 和 onMeasure 函数。在 onLayout 函数中,您可以覆盖宽度和高度。请参阅此处的示例 LabelView.java 类。