底部随输入法高度变化而变化的控件SoftLinearLayout

我们经常玩QQ、微信,大家是否认真看过它们的聊天界面,它们的输入框既可以随软键盘高度变化,又可以随底部控件的高度变化,而且底部控件还可以随软键盘高度的调整而自动调整(只不过设置了最小、最大值),看上去是不是觉得很酷呢?今天,我就在这介绍一个比它还好用的控件--SoftLinearLayout

首先,我们来看一下效果演示图:

SoftLinearLayout.gif

接下来,我们讲解一下控件功能及其使用:

1.功能

底部控件随输入法高度变化而变化,比QQ聊天界面更完美。

2.Android Studio使用方法

dependencies{
      compile 'com.wkp:SoftLinearLayout:1.0.1'
      //Android Studio3.0+可用以下方式
      //implementation 'com.wkp:SoftLinearLayout:1.0.1'
}

注意:以Github地址为准。

3.使用详解

  • 属性讲解
   <!--可变高度的极限小高度-->
  <attr name="wkp_minHeight" format="integer"/>
  <!--可变高度的极限大高度-->
  <attr name="wkp_maxHeight" format="integer"/>
  <!--显示软键盘时的动画时长-->
  <attr name="wkp_showSoftDuration" format="integer"/>
  <!--开关底部布局时的动画时长-->
  <attr name="wkp_toggleDuration" format="integer"/>
  • 布局示例
<?xml version="1.0" encoding="utf-8"?>
<com.wkp.softlinearlayout.view.SoftLinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/sll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/ll_top"
        android:orientation="vertical"
        android:background="@android:color/holo_blue_bright"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="0dp">

            <ListView
                android:id="@+id/lv"
                android:stackFromBottom="true"
                android:transcriptMode="normal"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            </ListView>

        </RelativeLayout>

        <EditText
            android:hint="你好"
            android:id="@+id/et"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <Button
            android:text="确定"
            android:onClick="sure"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_bottom"
        android:orientation="vertical"
        android:gravity="center"
        android:background="@android:color/holo_green_dark"
        android:layout_width="match_parent"
        android:layout_height="100dp">

        <TextView
            android:gravity="center"
            android:text="底部"
            android:textColor="@android:color/white"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </LinearLayout>

</com.wkp.softlinearlayout.view.SoftLinearLayout>
  • 代码示例
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public class MainActivity extends AppCompatActivity {

    private SoftLinearLayout mSll;
    private String[] mStrings = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("\\B");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView lv = findViewById(R.id.lv);
        lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,mStrings));
        mSll = findViewById(R.id.sll);
        //设置开关改变监听
        mSll.setOnToggleChangedListener(new SoftLinearLayout.OnToggleChangedListener() {
            @Override
            public void onToggleChanged(boolean isToggle) {
                Log.d("MainActivity", "isToggle:" + isToggle);
            }
        });
    }

    //点击开/关
    public void sure(View view) {
        mSll.toggle();
    }
}

结语

控件支持直接代码创建,还有更多API请观看SoftLinearLayout.java内的注释说明。

欢迎大家使用Github地址,感觉好用请给个Star鼓励一下,谢谢!

大家如果有更好的意见或建议以及好的灵感,请邮箱作者,谢谢!

QQ邮箱:
1535514884@qq.com

163邮箱:
15889686524@163.com

Gmail邮箱:
wkp15889686524@gmail.com

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 原文链接:https://github.com/opendigg/awesome-github-android-u...
    IM魂影阅读 32,971评论 6 472
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,595评论 25 708
  • 呵呵,这里有多少朋友已经将近奔三,或者已经在三字开头的路上呢? 我也就是即将奔三的人儿了,所以特意来感慨一下。。 ...
    浅蓝色的地阅读 123评论 0 1
  • 我曾有个朋友。 无所事事的时候我与一阳以因速度慢而著称的动物的速度在街上行走着,看美女,看屁股;看跑车,也看屁股。...
    ZLanZ阅读 241评论 0 1
  • 这堂笔记里的重点是:一怎么摆脱月光,二保险的规划。其中保险我会加上最近了解到的新内容。本节课的目的,为月光族...
    元气辣椒阅读 2,228评论 0 2