[英]How can I place a Button at the bottom of a layout and make it stay there even when keyboard opens?
嗨,以下是我的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#aaaaaa"
android:id="@+id/mainLayout"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_marginTop="55dp"
android:layout_height="wrap_content"
android:gravity="top">
<include layout="@layout/search_bar"/>
</LinearLayout>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:layout_height="50dp"
android:background="#eeeeee"
android:gravity="bottom">
<ImageButton
android:id="@+id/ImageButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginLeft="1dip"
android:layout_marginRight="1dip"
android:src="@drawable/appdrawer" />
</LinearLayout>
</LinearLayout>
打開布局時,ImageButton顯示在底部,但是當我將焦點放在SearchBox上時,鍵盤將打開,並且將ImageButton向上推。 如何使ImageButton保持頁腳。
您應該使用相對布局而不是線性布局,並添加android: layout_alignParentBottom="true"
。
從活動清單中添加set stateHidden|adjustpan
android:windowSoftInputMode
。
這是布局...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<include
android:id="@+id/activity_appointment_header"
// Your header If any
</include>
<ScrollView
android:id="@+id/scroll_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/activity_appointment_header"
android:layout_marginBottom="65dp"
android:background="@color/white"
android:fillViewport="true"
android:scrollbars="none" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
// Keep your layout
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="@+id/ll_book_appointment_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/scroll_view"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@color/cp_red" />
<Button
android:id="@+id/btn_book_appointment"
style="@style/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:text="@string/book_now" />
</LinearLayout>
</RelativeLayout>
並且您的活動應該在manifest.xml
是這樣的
<activity
android:name=".Activity"
android:windowSoftInputMode="stateAlwaysHidden|adjustResize" >
</activity>
嘗試將其添加到manifest.xml
<activity
android:name=".YourActivity"
android:configChanges="keyboardHidden|keyboard"
android:windowSoftInputMode="stateHidden|adjustNothing" >
</activity>
使用android:windowSoftInputMode =“ adjustPan”來顯示在相應的活動中:
<activity android:name="MyActivity"
...
android:windowSoftInputMode="adjustPan"
...
</activity>
用下面替換
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#aaaaaa"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:gravity="top"
android:orientation="horizontal" >
<include layout="@layout/search_bar" />
</LinearLayout>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
android:background="#eeeeee"
android:gravity="bottom"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/ImageButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginLeft="1dip"
android:layout_marginRight="1dip"
android:src="@drawable/appdrawer" />
</LinearLayout>
</LinearLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.