【问题标题】:Views shrink when using keyboard with PercentageLayout使用带有 PercentageLayout 的键盘时视图缩小
【发布时间】:2016-08-17 16:40:18
【问题描述】:

我有 2 个不同的 XML 文件。一个是 signup.xml,另一个是 chat.xml,两者都是用 PercentageLayout 库构建的。

signup.xml 效果很好 - 打开键盘不会改变任何视图。 但是,在 chat.xml 中,每当我弹出键盘时,视图都会缩小或超出屏幕,我不知道为什么,因为我几乎以相同的方式构造它们,只是我怀疑是因为我使用 @ 987654321@ 发生这种情况。

有问题的布局,chat.xml:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- top -->
<LinearLayout
    android:id="@+id/top"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_widthPercent="100%"
    app:layout_heightPercent="8%"
    android:background="#075607"
    android:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:textColor="#acddf4"
    android:textSize="20sp"
    android:text="jfioewjfoiewjfoi"
    android:gravity="center"
    android:layout_gravity="center_horizontal|center_vertical" />

</LinearLayout>
<!-- Mid -->
<LinearLayout
    android:id="@+id/mid"
    android:layout_below="@+id/top"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_widthPercent="100%"
    app:layout_heightPercent="84%"
    android:background="#b1b7b3"
    android:orientation="vertical">

    <ScrollView
        android:id="@+id/scroller"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/chat_bg" >

        <LinearLayout
            android:id="@+id/chatWindowContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>


    </LinearLayout>
<!-- Bottom -->
<LinearLayout
    android:id="@+id/bottom"
    android:layout_below="@+id/mid"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_widthPercent="100%"
    app:layout_heightPercent="8%"
    android:background="#000000"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/etMessage"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:ems="10"
        android:paddingLeft="5dp"
        android:background="@drawable/text"
        android:imeOptions="actionNone"
        android:inputType="textMultiLine"
        android:singleLine="false" />

    <Button
        android:id="@+id/bSendMessage"
        android:layout_width="40dp"
        android:layout_height="match_parent"
        android:background="@drawable/ic_send"
        android:layout_gravity="right" />



</LinearLayout>

还有工作布局,signup.xml:

<LinearLayout
    android:id="@+id/title"
    android:layout_height="0dp"
    android:layout_width="0dp"
    app:layout_heightPercent="15%"
    app:layout_widthPercent="100%">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/signup"/>
</LinearLayout>

<LinearLayout
    android:id="@+id/mid"
    android:orientation="vertical"
    android:layout_height="0dp"
    android:layout_width="0dp"
    android:layout_below="@+id/title"
    app:layout_heightPercent="35%"
    app:layout_widthPercent="100%"
    android:baselineAligned="true">
    <!-- Username -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:orientation="horizontal"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="20%">


        <ImageView
            android:layout_width="35dp"
            android:layout_height="wrap_content"
            android:src="@drawable/person" />
        <EditText
            android:id="@+id/etUsername_signup"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:ems="10"
            android:inputType="text"
            android:text="test"
            android:textColor="#000000"
            android:paddingLeft="5dp"
            android:layout_weight="0.1"
            android:background="@drawable/text">
            <requestFocus />
        </EditText>

    </LinearLayout>
    <!-- Email -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:orientation="horizontal"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="20%">


        <ImageView
            android:layout_width="35dp"
            android:layout_height="wrap_content"
            android:src="@drawable/email" />
        <EditText
            android:id="@+id/etEmail_signup"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:ems="10"
            android:inputType="textEmailAddress"
            android:text="email@address.com"
            android:textColor="#000000"
            android:paddingLeft="5dp"
            android:layout_weight="0.1"
            android:background="@drawable/text"/>

    </LinearLayout>
    <!-- Password1 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:orientation="horizontal"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="20%">


        <ImageView
            android:layout_width="35dp"
            android:layout_height="wrap_content"
            android:src="@drawable/password" />
        <EditText
            android:id="@+id/etPassword1_signup"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:ems="10"
            android:inputType="textPassword"
            android:text="pass"
            android:textColor="#000000"
            android:paddingLeft="5dp"
            android:layout_weight="0.1"
            android:background="@drawable/text"/>

    </LinearLayout>
    <!-- Password2 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:orientation="horizontal"
        app:layout_heightPercent="20%"
        app:layout_widthPercent="20%">


        <ImageView
            android:layout_width="35dp"
            android:layout_height="wrap_content"
            android:src="@drawable/password" />
        <EditText
            android:id="@+id/etPassword2_signup"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:ems="10"
            android:inputType="textPassword"
            android:text="pass"
            android:textColor="#000000"
            android:paddingLeft="5dp"
            android:layout_weight="0.1"
            android:background="@drawable/text"/>

    </LinearLayout>
    <!-- Spinner and RadioGroup-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">
        <Spinner
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:id="@+id/spinner"
            android:layout_gravity="center" />
        <RadioGroup
            android:id="@+id/rgRadius"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <RadioButton
                android:id="@+id/radio0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="Kilometer" />

            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Mile" />
        </RadioGroup>
    </LinearLayout>

</LinearLayout>
<!-- Image -->
<LinearLayout
    android:id="@+id/image"
    android:orientation="vertical"
    android:layout_height="0dp"
    android:layout_width="0dp"
    android:layout_below="@+id/mid"
    app:layout_heightPercent="40%"
    app:layout_widthPercent="100%"
    android:baselineAligned="true">

    <ImageView
        android:id="@+id/imgProfile"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/faceless" />

</LinearLayout>

<!-- Button -->
<LinearLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_heightPercent="10%"
    app:layout_widthPercent="100%"
    android:orientation="vertical"
    android:layout_below="@id/image">


    <Button
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:id="@+id/bSignup"
        android:layout_gravity="center|bottom"
        android:background="@drawable/button_send" />
</LinearLayout>

【问题讨论】:

  • 在 manifest.xml 中使用带有活动的 softinputmod?
  • 我不在任何地方使用软输入法
  • 嘿,我也遇到了同样的问题,你找到解决办法了吗?
  • 运气好吗?我也面临同样的问题

标签: java android xml user-interface


【解决方案1】:

使用相对布局解决了我在以下布局中测试的布局缩小问题。

使用活动标签添加以下条目:android:windowSoftInputMode="adjustResize"

还有你的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- top -->
    <LinearLayout
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="#075607"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textColor="#acddf4"
            android:textSize="20sp"
            android:text="jfioewjfoiewjfoi"
            android:gravity="center"
            android:layout_gravity="center_horizontal|center_vertical" />

    </LinearLayout>
    <!-- Mid -->
    <LinearLayout
        android:id="@+id/mid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom"
        android:layout_below="@+id/top"
        android:background="#b1b7b3"
        android:orientation="vertical">

        <ScrollView
            android:id="@+id/scroller"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
             >

            <LinearLayout
                android:id="@+id/chatWindowContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
            </LinearLayout>
        </ScrollView>


    </LinearLayout>
    <!-- Bottom -->
    <LinearLayout
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:background="#000000"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true">

        <EditText
            android:id="@+id/etMessage"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:ems="10"
            android:paddingLeft="5dp"
            android:imeOptions="actionNone"
            android:inputType="textMultiLine"
            android:singleLine="false" />

        <Button
            android:id="@+id/bSendMessage"
            android:layout_width="40dp"
            android:layout_height="match_parent"


            android:layout_gravity="right" />



    </LinearLayout>
    </RelativeLayout>

【讨论】:

  • 谢谢,但我真的很想要 PercentageLayout 的解决方案,因为 xml 这样更有意义。此外,我已经有一个可以正常工作的“常规”,但我想用 PercentageLayout 重建它
猜你喜欢
  • 1970-01-01
  • 2020-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-13
  • 2014-11-14
相关资源
最近更新 更多