以编程方式调整framelayout的大小

时间:2014-02-12 00:32:23

标签: android height android-framelayout

我有类似的东西:

  <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/photo1"
            android:orientation="vertical" >
     <FrameLayout
                android:id="@+id/photo2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

    </FrameLayout>
    <Fragment>

</Fragment>
    </FrameLayout>

我想调整photo2 FrameLayout的大小。

public void setScreenSize() {
    Display display = getWindowManager().getDefaultDisplay();
    FrameLayout layout = (FrameLayout) findViewById(R.id.photo2);
    int screen_height = display.getHeight();
    screen_height = (int) (0.60*screen_height);
    LayoutParams parms = (LayoutParams) layout.getLayoutParams();
    parms.height = screen_height;
    }

不幸的是,这不起作用,我不知道为什么。

2 个答案:

答案 0 :(得分:1)

您是否将高度设置回布局?

public void setScreenSize() {
    Display display = getWindowManager().getDefaultDisplay();
    FrameLayout layout = (FrameLayout) findViewById(R.id.photo2);
    int screen_height = display.getHeight();
    screen_height = (int) (0.60*screen_height);
    LayoutParams parms = (LayoutParams) layout.getLayoutParams();
    parms.height = screen_height;
    // Set it back.
    layout.setLayoutParams(parms);
}

答案 1 :(得分:0)

如果您想以编程方式更改FrameLyaout的大小,请复制并粘贴此代码,它的工作效率为100%。

FrameLayout frame1 = (FrameLayout) findViewById(R.id.framelayout1);
frame1.getLayoutParams().height =460;
frame1.getLayoutParams().width = 350;
frame1.requestLayout();