利用weight来实现因软键盘的出现,界面调整上移

最近要做这么一个界面,搞了很久,终于实现了.作为自己的笔记也好,分享也好,把方法写上来吧.

首先要做的界面是这样的,如下图:


界面上有个标题栏,是不动的.然后下面有个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这个可自己分配空间的属性. 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值