android菜鸟低调修行纪念册Android 基础知识Android开发经验谈

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

2017-12-29  本文已影响87人  wkp111

我们经常玩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

上一篇 下一篇

猜你喜欢

热点阅读