Android - 弹出键盘时调整片段大小

时间:2017-05-20 17:30:11

标签: android android-fragments keyboard

我的fragment包含表单,此fragment使用shape背景作为边框半径,当弹出键盘时,此片段会调整大小。

在键盘弹出here

之前来自fragment的此屏幕截图

这是键盘弹出here

的时候

这是我的片段布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_frm_jadwal_kuliah"
    android:background="@drawable/fragment_border"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
tools:context="me.citrafa.mycollegeassistant.Activity.Fragment.frmJadwalKuliah">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <me.citrafa.mycollegeassistant.CustomWidget.tvMuseo
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textAlignment="center"
        android:textColor="@color/accent"
        android:textSize="20sp"
        android:maxLines="2"
        android:text="Jadwal Kuliahku"/>
    <android.support.v7.widget.AppCompatSpinner
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/mySpinner"
        android:layout_marginTop="20dp"
        android:entries="@array/hari"
        android:id="@+id/spinnerHari"/>
    <me.citrafa.mycollegeassistant.CustomWidget.etMuseo
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textSize="20sp"
        android:id="@+id/txtMakul"
        style="@style/myEditTextForm"
        android:hint="Nama Matakuliah"/>
    <me.citrafa.mycollegeassistant.CustomWidget.etMuseo
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textSize="16sp"
        android:id="@+id/txtRuangan"
        style="@style/myEditTextForm"
        android:hint="Ruangan Kuliah"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <me.citrafa.mycollegeassistant.CustomWidget.etMuseo
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:textSize="16sp"
            android:id="@+id/txtJam"
            style="@style/myEditTextForm"
            android:hint="Jam Kuliah"/>
        <me.citrafa.mycollegeassistant.CustomWidget.etMuseo
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="5dp"
            android:textSize="16sp"
            android:id="@+id/txtKelas"
            style="@style/myEditTextForm"
            android:hint="Kelas"/>
    </LinearLayout>
    <me.citrafa.mycollegeassistant.CustomWidget.etMuseo
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:textSize="16sp"
        android:id="@+id/txtDosen"
        style="@style/myEditTextForm"
        android:hint="Nama Dosen"/>


</LinearLayout>

<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btnSimpan"
    app:srcCompat="@drawable/ic_done_white_24px"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="true"/>
</RelativeLayout>

这是形状&#34; fragment_border&#34; :

    <shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"   >

    <solid
        android:color="#fff" >
    </solid>

    <stroke
        android:width="1dp"
        android:color="#C4CDE0" >
    </stroke>

    <padding
        android:left="5dp"
        android:top="5dp"
        android:right="5dp"
        android:bottom="5dp"    >
    </padding>

    <corners
        android:radius="8dp"   >
    </corners>

</shape>

这是我如何调用片段:

fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Bundle bundle = new Bundle();
            bundle.putInt("idJK",0);
            frmJadwalKuliah f = new frmJadwalKuliah();
            f.setArguments(bundle);
            getFragmentManager().beginTransaction().add(R.id.tabJadwalKuliah,f).addToBackStack(null).commit();
            fab.hide();
        }
    });

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

此代码提供调整大小视图的帮助

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

}