最近要做这么一个界面,搞了很久,终于实现了.作为自己的笔记也好,分享也好,把方法写上来吧.
首先要做的界面是这样的,如下图:
界面上有个标题栏,是不动的.然后下面有个ImageView来显示图片,再下面有个输入框,最下面有个按钮.当点击输入框的时候,除了标题栏,其他都往上推,但是图片和输入框可以滑动的,以便查看.图比较丑,讲究着看吧.
以下是布局的XML代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/layoutBanner"
style="@style/ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical" >
<Button
android:id="@+id/btnBack"
android:layout_width="wrap_content"
android:layout_height="@dimen/actionbar_height"
android:background="@drawable/btn_back"
android:text="Button"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
<LinearLayout
android:id="@+id/fmInput"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1" >
<ScrollView
android:id="@+id/sView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="@+id/imgPic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/image"
android:src="@drawable/holo" />
<EditText
android:id="@+id/edtInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:singleLine="true" >
<requestFocus />
</EditText>
</LinearLayout>
</ScrollView>
</LinearLayout>
<Button
android:id="@+id/btnNext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/btn_default_disabled_holo_dark"
android:text="@string/btnNext" />
</LinearLayout>
关键就是那个fmInput的layout.(因业务需要,我本来是用frameLayout的,还有用到了ActionBar).把他设为fill parent,还有weight是1.用scrollView来包着imageview和edittext. 这个Activity的属性就是android:windowSoftInputMode="stateAlwaysHidden|adjustResize". 然后当点击输入框的时候,软键盘就会出现,把输入框和图片往上推了.而标题栏就不会动,还有底下的按钮也会在最下面,不会被软键盘覆盖到.原理就是利用weight这个可自己分配空间的属性.