当软键盘出现时,调整片段并保持活动稳定

时间:2017-02-28 07:40:11

标签: android android-fragments

我试图在软键盘出现时调整片段,但我希望activity_main.xml稳定,即不可调整。我试过这个,但它不起作用:

fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp">
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
        <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:scrollbars="none"
        android:id="@+id/me_info_scroll_view"
        >
            <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="10dp">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/name"
                        android:textSize="18sp"
                        android:textColor="@android:color/background_dark"/>
                    <TextView
                        android:id="@+id/user_name"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="20dp"
                        android:layout_marginTop="10dp"
                        android:textStyle="bold"
                        android:textColor="@android:color/background_dark"
                        />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/address"
                        android:textSize="18sp"
                        android:textColor="@android:color/background_dark"/>
                    <TextView
                        android:id="@+id/user_address"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="20dp"
                        android:layout_marginTop="10dp"
                        android:textStyle="bold"
                        android:textColor="@android:color/background_dark"
                        />

                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/home_phone"
                        android:textSize="18sp"
                        android:textColor="@android:color/background_dark"/>
                    <TextView
                        android:id="@+id/user_home_phone"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="20dp"
                        android:layout_marginTop="10dp"
                        android:textStyle="bold"
                        android:textColor="@android:color/background_dark"
                        />

                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/mobile_phone"
                        android:textSize="18sp"
                        android:textColor="@android:color/background_dark"/>
                    <TextView
                        android:id="@+id/user_mobile_phone"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="20dp"
                        android:layout_marginTop="10dp"
                        android:textStyle="bold"
                        android:textColor="@android:color/background_dark"
                        />

                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/work_phone"
                        android:textSize="18sp"
                        android:textColor="@android:color/background_dark"/>
                    <TextView
                        android:id="@+id/user_work_phone"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="20dp"
                        android:layout_marginTop="10dp"
                        android:textStyle="bold"
                        android:textColor="@android:color/background_dark"
                       />

                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/email"
                        android:textSize="18sp"
                        android:textColor="@android:color/background_dark"/>
                    <TextView
                        android:id="@+id/user_email"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="20dp"
                        android:layout_marginTop="10dp"
                        android:textStyle="bold"
                        android:textColor="@android:color/background_dark"
                        />

                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/birth_date"
                        android:textSize="18sp"
                        android:textColor="@android:color/background_dark"/>

                    <TextView
                        android:id="@+id/user_birth_date"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="20dp"
                        android:layout_marginTop="10dp"
                        android:textStyle="bold"
                        android:textColor="@android:color/background_dark"
                        />

                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/height"
                        android:textSize="18sp"
                        android:textColor="@android:color/background_dark"/>
                    <TextView
                        android:id="@+id/user_height"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="20dp"
                        android:layout_marginTop="10dp"
                        android:textStyle="bold"
                        android:textColor="@android:color/background_dark"
                        />

                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/weight"
                        android:textSize="18sp"
                        android:textColor="@android:color/background_dark"/>
                    <TextView
                        android:id="@+id/user_weight"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="20dp"
                        android:layout_marginTop="10dp"
                        android:textStyle="bold"
                        android:textColor="@android:color/background_dark"
                        />

                </LinearLayout>
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:gravity="center"
                    >
                    <Button
                        android:id="@+id/me_edit"
                        android:layout_width="100dp"
                        android:layout_height="wrap_content"
                        />
                </LinearLayout>
            </LinearLayout>
        </ScrollView>

    </LinearLayout>

</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:focusableInTouchMode="true"
    tools:openDrawer="start">

   <!-- <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />-->

    <!--framelayout to show other fragments-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="@color/navbar_color"
                android:orientation="horizontal"
                >

                <!-- android:theme="@style/AppTheme.AppBarOverlay"-->
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="?attr/actionBarSize"
                    android:layout_height="?attr/actionBarSize"

                    android:theme="@style/AppTheme.AppBarOverlay" />
                <TextView
                    android:id="@+id/tab_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="hello"
                    android:layout_gravity="center_vertical"
                    android:textColor="@android:color/white"
                    android:textSize="20sp"
                    android:layout_margin="10dp"
                    android:layout_weight="1"/>

                <ImageView
                    android:id="@+id/info"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:src="@drawable/info_icon"
                    android:layout_margin="8dp"
                    android:layout_gravity="center"
                    />
            </LinearLayout>

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/view_holder"
                android:layout_weight="1"
                />
        </LinearLayout>
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
      >
        <ListView
            android:id="@+id/navbar_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="@android:color/background_dark"
            android:dividerHeight="1dp">
        </ListView>
    </android.support.design.widget.NavigationView> 
</android.support.v4.widget.DrawerLayout>

mainactivity.java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);              
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);    
        setContentView(R.layout.activity_main);
        //checkPermission();
        // context of main Activity
        context = this;
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        appInfo = (ImageView) findViewById(R.id.info);
        navBarListView = (ListView) findViewById(R.id.navbar_list);
        title = (TextView) findViewById(R.id.tab_name);
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        setNavigationBar(true);
        navBarListView.setOnItemClickListener(this);

        //onclick listener for info icon
        appInfo.setOnClickListener(this);

        //getting db instance or initializing db
        emergencyDetailsDatabase = EmergencyDetailsDatabase.getDbInstance(context);
        emergencyDetailsDatabase.openDb();
        if(savedInstanceState == null)
        {
            FragmentManager fragmentManager = getFragmentManager();
        Fragment fragment = new Fragment1();
        fragmentManager.beginTransaction().replace(R.id.view_holder,fragment,"Edit user details").commit();
        }
    }

    fragment.java
    public class Fragment1 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //getActivity().getWindow().setSoftInputMode();   //getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                //WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);      getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        view = inflater.inflate(R.layout.fragment, container, false);      
        return  view;
    }

androidmanifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.benayah.emergencyapp">
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
            android:windowSoftInputMode="stateVisible|adjustResize"
            android:configChanges="screenSize|touchscreen|keyboard|keyboardHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest> 

我希望在输入键盘出现时可以调整片段,我希望片段的按钮显示在softInputmode上。

1 个答案:

答案 0 :(得分:0)

尝试在清单中定义软输入模式(android:windowSoftInputMode =“adjustResize”)

 <activity
       android:name="..MainActivity"
       android:windowSoftInputMode="adjustResize">
       <intent-filter>
           <action android:name="android.intent.action.MAIN" />
           <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>