有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在运行时更改视图大小

如果方向在运行时发生变化,我想将片段并排显示。我所做的是声明两个框架布局,第二个尺寸在高度和宽度上为零

<安卓.support.v4.widget.DrawerLayout
    xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:id="@+id/drawer_layout"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent">

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


    <!-- 安卓: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
        安卓:id="@+id/left_drawer"
        安卓:layout_width="240dp"
        安卓:layout_height="match_parent"
        安卓:layout_gravity="start"
        安卓:choiceMode="singleChoice"
        安卓:divider="@安卓:color/transparent"
        安卓:dividerHeight="0dp"
        安卓:background="#111"/>
</安卓.support.v4.widget.DrawerLayout>

然后,当方向改变时,我尝试调整框架布局的大小。不是每个片段占据屏幕的一半,而是重叠并占据整个屏幕。那么,为什么两者都占据整个屏幕,而不是占据屏幕的一半呢

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;
                contentFrame.setX(portriatHeight/2);
                contentFrame.setY(portraitWidth/2);
                contentFrame.requestLayout();
                contentFrame.invalidate();

共 (1) 个答案

  1. # 1 楼答案

    如果你在屏幕上显示了决定性数量的碎片,看起来你会这样做

    我强烈建议只创建一个布局景观变化。系统将注意到方向发生了变化,并查找相关布局(位于res的正下方)>;布局土地文件夹

    如果你没有,并且正在使用Android Studio,只需在设计模式下打开布局,找到带有Android头部的文件图标,点击该图标,然后选择“创建景观布局”