Android弹出软键盘引起的界面错乱:控件上移下移

情景是这样:Dialog里面EditText弹出软键盘,结果主界面里面的两个TextView居然莫名其妙的下移了。

引起错乱的布局如下

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="3dp">

        <TextView
            android:id="@+id/t_blue"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_margin="3dp"
            android:layout_weight="1"
            android:background="@drawable/round_grey"
            android:gravity="center"
            android:text="蓝牙连接"
            android:textSize="12dp" />

        <TextView
            android:id="@+id/t_plc"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_margin="3dp"
            android:layout_weight="1"
            android:background="@drawable/round_grey"
            android:gravity="center"
            android:text="PLC连接"
            android:textSize="12dp" />

        <TextView
            android:id="@+id/t_http"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_margin="3dp"
            android:layout_weight="1"
            android:background="@drawable/round_grey"
            android:gravity="center"
            android:text="网络连接"
            android:textSize="12dp" />
    </LinearLayout>

在将后两个TextView设置两行内容的时候,弹出软键盘,TextView位置会向下移。经过一番查阅也没有找到合适的解决方法,只能各种尝试。

最终解决办法:

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="57dp"
        android:orientation="horizontal"
        android:padding="3dp">

        <TextView
            android:id="@+id/t_blue"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="3dp"
            android:layout_weight="1"
            android:background="@drawable/round_grey"
            android:gravity="center"
            android:text="蓝牙连接"
            android:textSize="12dp" />

        <TextView
            android:id="@+id/t_plc"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="3dp"
            android:layout_weight="1"
            android:background="@drawable/round_grey"
            android:gravity="center"
            android:text="PLC连接"
            android:textSize="12dp" />

        <TextView
            android:id="@+id/t_http"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="3dp"
            android:layout_weight="1"
            android:background="@drawable/round_grey"
            android:gravity="center"
            android:text="网络连接"
            android:textSize="12dp" />
    </LinearLayout>

将设置TextView高度改为设置LinearLayout的高度,问题就解决了。具体为什么会引起界面错乱,暂时还没有弄明白,如果有大牛路过顺便来讲解一下。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对您的问题,我可以给您一些思路和方案。在 Android 中,当软键盘弹出时,系统会自动调整界面布局以适应软键盘的高度,但这种调整并不一定是我们期望的,因此需要我们手动进行调整。 一种常见的做法是在 AndroidManifest.xml 文件中对当前 Activity 进行设置,添加如下代码: ``` android:windowSoftInputMode="adjustResize" ``` 这样系统就会在软键盘弹出时自动调整布局大小,但是这种方式可能会出现一些问题,比如界面元素重叠,无法滚动等等。 另一种做法是在代码中手动监听软键盘弹出和隐藏事件,在事件回调中进行布局的调整。通常可以通过监听 ViewTreeObserver 的 OnGlobalLayoutListener 事件来实现。具体的实现方式可以参考如下代码: ```java // 获取根布局 View rootView = findViewById(android.R.id.content); // 监听布局变化 rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { Rect rect = new Rect(); rootView.getWindowVisibleDisplayFrame(rect); // 计算软键盘高度 int keyboardHeight = rootView.getHeight() - rect.bottom; // 如果软键盘弹出 if (keyboardHeight > 0) { // 上移布局 rootView.setTranslationY(-keyboardHeight); } else { // 恢复布局 rootView.setTranslationY(0); } } }); ``` 这样就可以在软键盘弹出时将布局整体上移软键盘收起时恢复布局。当然,具体的实现方式还需要根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值