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

6

我是 Android 的新手。

我遇到了一个问题,就是我在我的应用程序中添加了 FrameLayout,代码如下:

<?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));
        }

但它会强制关闭,

如果有任何想法,请帮助我。

提前致谢。

2个回答

3

您需要使用LinearLayout.LayoutParams(int width,int height,float weight)

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接