zoukankan      html  css  js  c++  java
  • Android 布局 中实现适应屏幕大小及组件滚动

    要实现如图的布局:

    这是在eclipse可视化窗口中的截图,但实际运行在Android设备上可能出现的问题有:

    (1):当编辑图1中的最后一个EditText时,输入法的编辑界面会把底部的Button顶上去,遮挡住了最后一个EditText;

    (2):图2中的控件太多,以至于无法一屏全部显示,导致底部的Button被挤下去,又无法滚动控件,导致无法操作Button;

    解决方案:

    以下为布局文件代码:

    图  1:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    
        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/linearLayout1"
            android:fillViewport="true" >
    
            <TableLayout
                android:id="@+id/table"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:stretchColumns="1" >
    
                <TableRow>
    
                    <TextView
                        android:layout_width="60dp"
                        android:layout_height="wrap_content"
                        android:text="@string/product_name" />
    
                    <EditText
                        android:layout_height="wrap_content"
                        android:hint="@string/product_name" >
                    </EditText>
                </TableRow>
    
                <TableRow>
    
                    <TextView
                        android:layout_width="60dp"
                        android:layout_height="wrap_content"
                        android:text="@string/product_id" />
    
                    <EditText
                        android:layout_height="wrap_content"
                        android:hint="@string/product_id" >
                    </EditText>
                </TableRow>
    
                <TableRow>
    
                    <TextView
                        android:layout_width="60dp"
                        android:layout_height="wrap_content"
                        android:text="@string/raise_company" />
    
                    <EditText
                        android:layout_height="wrap_content"
                        android:hint="@string/raise_company" >
                    </EditText>
                </TableRow>
    
                <TableRow>
    
                    <TextView
                        android:layout_width="80dp"
                        android:layout_height="wrap_content"
                        android:text="@string/about_info" />
    
                    <EditText
                        android:layout_height="wrap_content"
                        android:layout_weight="2"
                        android:hint="@string/about_quality_website" >
                    </EditText>
                </TableRow>
            </TableLayout>
        </ScrollView>
    
        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:paddingBottom="0dp" >
    
            <Button
                android:layout_width="70dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:text="@string/edit" />
    
            <Button
                android:layout_width="70dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:text="@string/refer" >
            </Button>
        </LinearLayout>
    
    </RelativeLayout>
    View Code

    图  2:

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
    
            <ScrollView
                android:id="@+id/view1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >
    
                <TableLayout
                    android:id="@+id/table"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:stretchColumns="1" >
    
                    <TableRow>
    
                        <TextView
                            android:layout_width="60dp"
                            android:layout_height="wrap_content"
                            android:text="@string/product_name" />
    
                        <EditText
                            android:layout_height="wrap_content"
                            android:hint="@string/product_name" >
                        </EditText>
                    </TableRow>
    
                    <TableRow>
    
                        <TextView
                            android:layout_width="60dp"
                            android:layout_height="wrap_content"
                            android:text="@string/product_id" />
    
                        <EditText
                            android:layout_height="wrap_content"
                            android:hint="@string/product_id" >
                        </EditText>
                    </TableRow>
    
                    <TableRow>
    
                        <TextView
                            android:layout_width="60dp"
                            android:layout_height="wrap_content"
                            android:text="@string/current_data" />
    
                        <EditText
                            android:layout_height="wrap_content"
                            android:hint="@string/current_data" >
                        </EditText>
                    </TableRow>
    
                    <TableRow>
    
                        <TextView
                            android:layout_width="60dp"
                            android:layout_height="wrap_content"
                            android:text="@string/raise_company" />
    
                        <EditText
                            android:layout_height="wrap_content"
                            android:hint="@string/raise_company" >
                        </EditText>
                    </TableRow>
    
                    <TableRow>
    
                        <TextView
                            android:layout_width="60dp"
                            android:layout_height="wrap_content"
                            android:text="@string/raise_man" />
    
                        <EditText
                            android:layout_height="wrap_content"
                            android:hint="@string/raise_man" >
                        </EditText>
                    </TableRow>
    
                    <TableRow>
    
                        <TextView
                            android:layout_width="60dp"
                            android:layout_height="wrap_content"
                            android:text="@string/feed_name" />
    
                        <EditText
                            android:layout_height="wrap_content"
                            android:hint="@string/feed_name" >
                        </EditText>
                    </TableRow>
    
                    <TableRow>
    
                        <TextView
                            android:layout_width="60dp"
                            android:layout_height="wrap_content"
                            android:text="@string/feed_element" />
    
                        <EditText
                            android:layout_height="wrap_content"
                            android:hint="@string/feed_element" >
                        </EditText>
                    </TableRow>
    
                    <TableRow>
    
                        <TextView
                            android:layout_width="60dp"
                            android:layout_height="wrap_content"
                            android:text="@string/medicine" />
    
                        <EditText
                            android:layout_height="wrap_content"
                            android:hint="@string/medicine" >
                        </EditText>
                    </TableRow>
    
                    <TableRow>
    
                        <TextView
                            android:layout_width="60dp"
                            android:layout_height="wrap_content"
                            android:text="@string/health_condition" />
    
                        <EditText
                            android:layout_height="wrap_content"
                            android:hint="@string/health_condition" >
                        </EditText>
                    </TableRow>
    
                    <TableRow>
    
                        <TextView
                            android:layout_width="60dp"
                            android:layout_height="wrap_content"
                            android:text="@string/water_quality" />
    
                        <EditText
                            android:layout_height="wrap_content"
                            android:hint="@string/water_quality" >
                        </EditText>
                    </TableRow>
    
                    <TableRow>
    
                        <TextView
                            android:layout_width="60dp"
                            android:layout_height="wrap_content"
                            android:text="@string/water_temperature" />
    
                        <EditText
                            android:layout_height="wrap_content"
                            android:hint="@string/water_temperature" >
                        </EditText>
                    </TableRow>
    
                    <TableRow>
    
                        <TextView
                            android:layout_width="60dp"
                            android:layout_height="wrap_content"
                            android:text="@string/record" />
    
                        <EditText
                            android:layout_height="wrap_content"
                            android:hint="@string/record" >
                        </EditText>
                    </TableRow>
                </TableLayout>
            </ScrollView>
    
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/view1" >
    
                <Button
                    android:layout_width="70dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:text="@string/edit" />
    
                <Button
                    android:layout_width="70dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:text="@string/refer" >
                </Button>
            </LinearLayout>
        </RelativeLayout>
    
    </ScrollView>
    View Code

    需要说明的是:图一结构为:

    图二结构为:

    图二没有遮挡的问题,所以没有去修改它

    结束语:关于Android中布局文件的滚动和控件遮挡问题已经描述和解决,作为一篇记录学习过程的文章,也希望能够帮助别人一起学习。

               附上的代码,如果你喜欢,欢迎拿去,顺便,如果你把它完善的根据完美漂亮,也希望你反馈到这里  ^_^

  • 相关阅读:
    生成随机数
    ES集群开启X-pack认证
    部署ceph
    分布式存储ceph理论
    kvm虚拟机迁移
    kvm虚拟机网络管理
    kvm虚拟机存储管理
    机电传动控制个人课程报告
    第11周机电传动控制作业
    补充第九周手工画图
  • 原文地址:https://www.cnblogs.com/conncui/p/Layout.html
Copyright © 2011-2022 走看看