如何在运行时调整视图大小

时间:2014-09-01 09:58:37

标签: java android

在我的应用中,我有两个listfragments。如果我在列表中选择一个项目,则listfragment将被另一个片段替换。如果方向是横向的,我想按尺寸(宽度相等)显示两个碎片。

if((new Configuration()).orientation == Configuration.ORIENTATION_PORTRAIT){
    selectItem(savedInstanceState.getInt("fragmentShowing"));
} else{
        FrameLayout frame = (FrameLayout) findViewById(R.id.content_frame);
        int portraitWidth,portriatHeight;
        portraitWidth = frame.getWidth();
        portriatHeight = frame.getHeight();
        frame.getLayoutParams().width = portriatHeight/2;
        frame.getLayoutParams().height = portraitWidth/2;
        frame.setX(0);
        frame.setY(0);
        frame.requestLayout();
        frame.invalidate();

        FrameLayout contentFrame = (FrameLayout) findViewById(R.id.conversation_history);
        contentFrame.getLayoutParams().width = portriatHeight/2;
        contentFrame.getLayoutParams().height = portraitWidth/2;
        frame.setX(portriatHeight/2);
        frame.setY(portraitWidth/2);
        contentFrame.requestLayout();
        contentFrame.invalidate();
}

我已更新代码以反映我的进度。现在它可以工作,但它们重叠并且都占据了整个屏幕的空间     

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <FrameLayout
        android:id="@+id/conversation_history"
        android:layout_width="0px"
        android:layout_height="0px"/>


    <!-- android:layout_gravity="start" tells DrawerLayout to treat
         this as a sliding drawer on the left side for left-to-right
         languages and on the right side for right-to-left languages.
         The drawer is given a fixed width in dp and extends the full height of
         the container. A solid background is used for contrast
         with the content view. -->
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

1 个答案:

答案 0 :(得分:0)

试试这个

View mView = findViewById(R.id.my_view)
layout.setLayoutParams(new LinearLayout.LayoutParams(100,100));