0

这是我的第一个 android 应用程序,对此非常陌生。我的应用在移动设备中使用时,布局未完全显示。我的意思是说最后的按钮不显示。使用 ScrollView 将解决这个问题。但是有没有办法根据我使用的设备调整布局?还有一个使用 ScrollView 的示例代码将不胜感激。提前致谢。

编辑:布局代码(.xml)

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Matchdetails" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="15dp"
        android:text="@string/Name" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignParentRight="true"
        android:ems="10"
        android:inputType="text" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="@string/Maledet" />

    <Spinner
        android:id="@+id/spin_ras"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_toLeftOf="@+id/textView1" />

    <Spinner
        android:id="@+id/spin_nak"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/spin_ras"
        android:layout_below="@+id/spin_ras"
        android:layout_toLeftOf="@+id/textView1" />

    <Spinner
        android:id="@+id/spin_gotra"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/spin_nak"
        android:layout_toRightOf="@+id/textView1" />

    <Spinner
        android:id="@+id/spin_pada"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/spin_nak"
        android:layout_toRightOf="@+id/textView1" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView4"
        android:layout_alignBottom="@+id/textView4"
        android:layout_alignRight="@+id/spin_pada"
        android:ems="10"
        android:inputType="text" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/spin_nak"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="14dp"
        android:text="@string/Femdet" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/spin_nak"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="14dp"
        android:text="@string/Name" />

    <Spinner
        android:id="@+id/spin_fras"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView4"
        android:layout_below="@+id/editText2"
        android:layout_marginTop="14dp"
        android:layout_toLeftOf="@+id/textView1" />

    <Spinner
        android:id="@+id/spin_fnak"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/spin_fras"
        android:layout_below="@+id/spin_fras"
        android:layout_toLeftOf="@+id/textView1" />

    <Spinner
        android:id="@+id/spin_fpada"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/spin_fnak"
        android:layout_toRightOf="@+id/textView1" />

    <Spinner
        android:id="@+id/spin_fgotra"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/spin_fpada"
        android:layout_toRightOf="@+id/textView1" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/spin_fnak"
        android:layout_marginLeft="15dp"
        android:layout_toRightOf="@+id/textView4"
        android:text="@string/Submit" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button1"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@+id/button1"
        android:text="@string/Cancel" />

</RelativeLayout>
4

3 回答 3

1

您可能还想尝试兼容性布局:)

在此处输入图像描述

于 2013-11-01T06:46:43.597 回答
0
// try this
<?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:gravity="center"
    android:padding="5dp"
    android:orientation="vertical">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_marginTop="10dp"
                android:layout_height="wrap_content"
                android:text="@string/Maledet" />

            <LinearLayout
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_width="match_parent">

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:text="@string/Name" />

                <EditText
                    android:id="@+id/editText1"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:inputType="text" />
            </LinearLayout>

            <LinearLayout
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_width="match_parent">
                <Spinner
                    android:id="@+id/spin_ras"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"/>

                <Spinner
                    android:id="@+id/spin_gotra"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"/>
            </LinearLayout>

            <LinearLayout
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_width="match_parent">

                <Spinner
                    android:id="@+id/spin_nak"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"/>

                <Spinner
                    android:id="@+id/spin_pada"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"/>
                </LinearLayout>

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="@string/Femdet" />

            <LinearLayout
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_width="match_parent">

                <TextView
                    android:id="@+id/textView4"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:text="@string/Name" />

                <EditText
                    android:id="@+id/editText2"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:inputType="text" />
                </LinearLayout>
            <LinearLayout
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_width="match_parent">

                <Spinner
                    android:id="@+id/spin_fras"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"/>

                <Spinner
                    android:id="@+id/spin_fgotra"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"/>

            </LinearLayout>

            <LinearLayout
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_width="match_parent">
                <Spinner
                    android:id="@+id/spin_fnak"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"/>

                <Spinner
                    android:id="@+id/spin_fpada"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"/>
                </LinearLayout>

        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/spin_fnak"
            android:layout_margin="5dp"
            android:text="@string/Submit" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_toRightOf="@id/button1"
            android:text="@string/Cancel" />
    </LinearLayout>
</LinearLayout>
于 2013-11-01T03:48:03.403 回答
0

I have modified your layout and put it in a scroll view.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".Matchdetails" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/textView1"
            android:layout_marginTop="15dp"
            android:text="@string/Name" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/textView2"
            android:layout_alignParentRight="true"
            android:ems="10"
            android:inputType="text" >
        </EditText>

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:text="@string/Maledet" />

        <Spinner
            android:id="@+id/spin_ras"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/textView2"
            android:layout_below="@id/textView2"
            android:layout_toLeftOf="@id/textView1" />

        <Spinner
            android:id="@+id/spin_nak"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/spin_ras"
            android:layout_below="@id/spin_ras"
            android:layout_toLeftOf="@id/textView1" />

        <Spinner
            android:id="@+id/spin_gotra"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@id/spin_nak"
            android:layout_toRightOf="@id/textView1" />

        <Spinner
            android:id="@+id/spin_pada"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@id/spin_nak"
            android:layout_toRightOf="@id/textView1" />

        <EditText
            android:id="@+id/editText2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/textView4"
            android:layout_alignBottom="@+id/textView4"
            android:layout_alignRight="@id/spin_pada"
            android:ems="10"
            android:inputType="text" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/spin_nak"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="14dp"
            android:text="@string/Femdet" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/spin_nak"
            android:layout_below="@id/textView3"
            android:layout_marginTop="14dp"
            android:text="@string/Name" />

        <Spinner
            android:id="@+id/spin_fras"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView4"
            android:layout_below="@id/editText2"
            android:layout_marginTop="14dp"
            android:layout_toLeftOf="@id/textView1" />

        <Spinner
            android:id="@+id/spin_fnak"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/spin_fras"
            android:layout_below="@id/spin_fras"
            android:layout_toLeftOf="@id/textView1" />

        <Spinner
            android:id="@+id/spin_fpada"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@id/spin_fnak"
            android:layout_toRightOf="@id/textView1" />

        <Spinner
            android:id="@+id/spin_fgotra"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@id/spin_fpada"
            android:layout_toRightOf="@id/textView1" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/spin_fpada"
            android:layout_centerHorizontal="true" >

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/spin_fnak"
                android:layout_margin="5dp"
                android:text="@string/Submit" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_toRightOf="@id/button1"
                android:text="@string/Cancel" />
        </LinearLayout>
    </RelativeLayout>

</ScrollView>

A couple of things to be noted.

  1. You need @+id/view_id only when you create a new View ID for the first time in your layout. When you refer an existing id you can do so with @id/view_id.

  2. Avoid hard coding the values wherever possible. You will find it difficult while using your layout to fit different screens.

  3. The above layout isn't perfect, but will fit your requirement. You may want to use nested views in your layout and get the hang of what the different attributes mean in the layout xml. That, you will gain by trying out different things.

In general, Designing for multiple screens involves considering a number of factors.

  1. Supporting screens with different sizes and resolutions (VGA/ HVGA / WVGA/ WXGA) screens/(hdpi/mdpi..) densities.
  2. Different Form factors(smartphones/tablets)
  3. Different orientations(landscape/portrait) on the same device.

You should perhaps read this for a complete know-how about designing your app for multiple screen types. And there's a sample application in the link.

于 2013-11-01T02:14:33.147 回答