1

我已经写了这段代码,但我遇到了问题,因为相对布局高度没有包裹内容,请帮助我解决这个问题我手动给出高度,但我希望它根据内容而变化

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relativelayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/Background"
    tools:context=".JewelInfo" >

<ImageView
    android:id="@+id/HeadIm1"
    android:layout_width="60dp"
    android:layout_height="30dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@drawable/Untitled"
    android:layout_marginLeft="10dp" />

<ImageView
    android:id="@+id/HeadIm2"
    android:layout_width="120dp"
    android:layout_height="30dp"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="10dp"
    android:layout_toRightOf="@+id/HeadIm1"
    android:background="@drawable/myj"
    android:paddingLeft="10dp"
    android:layout_marginBelow="5dp"
    android:paddingBottom="5dp" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:layout_below="@+id/HeadIm1"
    android:background="@drawable/menu"
    android:gravity="center"
    android:text="JEWELLERY DETAILS"
    android:textColor="#000000"
    android:textColorHighlight="#000000"
    android:textSize="20dp" />

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:background="@drawable/Background" >


    <RelativeLayout
        android:id="@+id/rlayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/Info"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16sp"
            android:layout_marginRight="16sp"
            android:layout_marginTop="16sp"
            android:text="Fill in the details about gold jewellery that you would like accessed &amp; Find out the current market value"
            android:textColor="#CFB52B" />

        <RelativeLayout
            android:id="@+id/rlayout1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/Info"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginTop="15dp"
            android:background="@drawable/menubar" >

            <Spinner
                android:id="@+id/Jtype"
                android:layout_width="95dp"
                android:layout_height="30dp"
                android:layout_alignLeft="@+id/JName"
                android:layout_below="@+id/JName"
                android:background="@drawable/Black"
                android:text="Jewellery Type"
                android:textColor="#FFFFFF"
                android:textSize="18dp" />

            <Spinner
                android:id="@+id/weight"
                android:layout_width="95dp"
                android:layout_height="30dp"
                android:layout_alignBaseline="@+id/Jtype"
                android:layout_alignBottom="@+id/Jtype"
                android:layout_alignRight="@+id/JName"
                android:background="@drawable/Black"
                android:hint="Karat"
                android:textColor="#FFFFFF"
                android:textSize="18dp" />

                <Button
                    android:id="@+id/browse"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:layout_margin="10dp"
                    android:layout_marginTop="18dp"
                    android:hint="Browse Image"
                    android:textSize="20dp" 
                    android:background="@drawable/Black" />

                <EditText
                    android:id="@+id/JWeight"
                    android:layout_width="200dp"
                    android:layout_height="30dp"
                    android:layout_alignBelow="@+id/browse"
                    android:layout_alignLeft="@+id/Jtype"
                    android:layout_below="@+id/Jtype"
                    android:layout_marginRight="11dp"
                    android:layout_marginTop="11dp"
                    android:layout_toRightof="@id/browse"
                    android:background="@drawable/Black"
                    android:ems="10"
                    android:hint="Enter Gold Weight (gms)"
                    android:textColor="#000000"
                    android:textSize="16dp"
                    android:layout_marginBottom="11dp" />


              <EditText
                    android:id="@+id/JName"
                    android:layout_width="200dp"
                    android:layout_height="30dp"
                    android:layout_alignParentRight="true"
                    android:layout_alignTop="@+id/browse"
                    android:layout_marginRight="12dp"
                    android:layout_marginBottom="11dp"
                    android:layout_toRightof="@id/browse"
                    android:background="@drawable/Black"
                    android:ems="10"
                    android:hint="Jewellery Name"
                    android:textColor="#000000"
                    android:textSize="16dp" />
                      <requestFocus />
            </RelativeLayout>

        </RelativeLayout>
    </ScrollView>

</RelativeLayout> 
4

4 回答 4

0

你可以尝试添加

android:layout_alignParentTop="true"

到 TextView“信息”。这可能会对您有所帮助。

于 2013-09-24T11:45:33.420 回答
0

在相对于彼此放置视图时,您必须使用 @+id 而不是 @id

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relativelayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/Background"
    tools:context=".JewelInfo" >

    <ImageView
        android:id="@+id/HeadIm1"
        android:layout_width="60dp"
        android:layout_height="30dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10dp"
        android:background="@drawable/Untitled" />

    <ImageView
        android:id="@+id/HeadIm2"
        android:layout_width="120dp"
        android:layout_height="30dp"
        android:layout_alignParentTop="true"
        android:layout_marginBelow="5dp"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/HeadIm1"
        android:background="@drawable/myj"
        android:paddingBottom="5dp"
        android:paddingLeft="10dp" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_below="@+id/HeadIm1"
        android:background="@drawable/menu"
        android:gravity="center"
        android:text="JEWELLERY DETAILS"
        android:textColor="#000000"
        android:textColorHighlight="#000000"
        android:textSize="20dp" />

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:background="@drawable/Background" >

        <RelativeLayout
            android:id="@+id/rlayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/Info"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16sp"
                android:layout_marginRight="16sp"
                android:layout_marginTop="16sp"
                android:text="Fill in the details about gold jewellery that you would like accessed &amp; Find out the current market value"
                android:textColor="#CFB52B" />

            <RelativeLayout
                android:id="@+id/rlayout1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/Info"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/menubar" >

                <Spinner
                    android:id="@+id/Jtype"
                    android:layout_width="95dp"
                    android:layout_height="30dp"
                    android:layout_alignLeft="@+id/JName"
                    android:layout_below="@+id/JName"
                    android:background="@drawable/Black"
                    android:text="Jewellery Type"
                    android:textColor="#FFFFFF"
                    android:textSize="18dp" />

                <Spinner
                    android:id="@+id/weight"
                    android:layout_width="95dp"
                    android:layout_height="30dp"
                    android:layout_alignBaseline="@+id/Jtype"
                    android:layout_alignBottom="@+id/Jtype"
                    android:layout_alignRight="@+id/JName"
                    android:background="@drawable/Black"
                    android:hint="Karat"
                    android:textColor="#FFFFFF"
                    android:textSize="18dp" />

                <Button
                    android:id="@+id/browse"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:layout_margin="10dp"
                    android:layout_marginTop="18dp"
                    android:background="@drawable/Black"
                    android:hint="Browse Image"
                    android:textSize="20dp" />

                <EditText
                    android:id="@+id/JWeight"
                    android:layout_width="200dp"
                    android:layout_height="30dp"
                    android:layout_alignBelow="@+id/browse"
                    android:layout_alignLeft="@+id/Jtype"
                    android:layout_below="@+id/Jtype"
                    android:layout_marginBottom="11dp"
                    android:layout_marginRight="11dp"
                    android:layout_marginTop="11dp"
                    android:layout_toRightof="@+id/browse"
                    android:background="@drawable/Black"
                    android:ems="10"
                    android:hint="Enter Gold Weight (gms)"
                    android:textColor="#000000"
                    android:textSize="16dp" />

                <EditText
                    android:id="@+id/JName"
                    android:layout_width="200dp"
                    android:layout_height="30dp"
                    android:layout_alignParentRight="true"
                    android:layout_alignTop="@+id/browse"
                    android:layout_marginBottom="11dp"
                    android:layout_marginRight="12dp"
                    android:layout_toRightof="@+id/browse"
                    android:background="@drawable/Black"
                    android:ems="10"
                    android:hint="Jewellery Name"
                    android:textColor="#000000"
                    android:textSize="16dp" />

                <requestFocus />
            </RelativeLayout>
        </RelativeLayout>
    </ScrollView>

</RelativeLayout>
于 2013-09-24T11:45:46.977 回答
0

您正在使用 ScrollView。您需要对其高度进行硬编码,以便内容可以在该特定高度内滚动。如果您说 wrap_content ,那么基本上它将扩展以填充所有内容,而无需为您提供滚动选项。

此外,如果您愿意,您可以在运行时动态设置 ScrollView 的高度:-

sv = (ScrollView) findViewByID(R.id.scrollView1);

sv.getLayoutParams().height = x;
sv.getLayoutParams().width = x;                //just for information, you don't need it

sv.setLayoutParams(sv.getLayoutParams);

使用此代码,您可以设置 ScrollView 的高度和宽度。将高度设置为小于 ScrollView 内部视图所需的总高度将使其可滚动。

此外,您可以获取屏幕的大小,然后相应地设置 ScrollView 的大小:-

API<13

Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight(); //not required, just to inform

int x = (int) screenWidth/10;      //replace 10 by scaling factor

sv = (ScrollView) findViewByID(R.id.scrollView1);

sv.getLayoutParams().height = x;
sv.getLayoutParams().width = x;    //just for information, you don't need it

sv.setLayoutParams(sv.getLayoutParams);

对于 API>=13

Point size = new Point();
display.getSize(size);
int screenWidth = size.x;
int screenHeight = size.y;         //not required, just for info

int x = (int) screenWidth/10; //replace 10 by scaling factor
sv = (ScrollView) findViewByID(R.id.scrollView1);

sv.getLayoutParams().height = x;
sv.getLayoutParams().width = x;     //just for information, you don't need it

sv.setLayoutParams(sv.getLayoutParams);

要在运行时检查 API 级别,请使用

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {

         //<code> if API>=13 (HONEYCOMB_MR2 is API 13)
}

else {

         //<code> if API<13
}

希望能帮助到你。快乐编程:)

于 2013-09-24T14:12:39.187 回答
0
// try this
<?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="wrap_content"
              android:orientation="vertical"
              android:background="@drawable/Background"
              tools:context=".JewelInfo" >

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="5dp">

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            <ImageView
                    android:id="@+id/HeadIm1"
                    android:layout_width="60dp"
                    android:layout_height="30dp"
                    android:background="@drawable/Untitled"/>

            <ImageView
                    android:id="@+id/HeadIm2"
                    android:layout_width="120dp"
                    android:layout_height="30dp"
                    android:layout_alignParentTop="true"
                    android:background="@drawable/myj"
                    android:layout_marginLeft="20dp"/>

        </LinearLayout>

        <TextView
                android:id="@+id/textView1"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:gravity="center"
                android:text="JEWELLERY DETAILS"
                android:textColor="#000000"
                android:textColorHighlight="#000000"
                android:background="@drawable/menu"
                android:textSize="20dp" />
    </LinearLayout>

    <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="@drawable/Background"
            android:layout_weight="1">


        <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:orientation="vertical">

            <TextView
                    android:id="@+id/Info"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Fill in the details about gold jewellery that you would like accessed &amp; Find out the current market value"
                    android:textColor="#CFB52B" />

            <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:layout_marginTop="10dp"
                    android:background="@drawable/menubar">

                <Button
                        android:id="@+id/browse"
                        android:layout_width="100dp"
                        android:layout_height="100dp"
                        android:hint="Browse Image"
                        android:background="@drawable/Black"
                        android:textSize="20dp"/>

                <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:orientation="vertical">
                    <EditText
                            android:id="@+id/JName"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:ems="10"
                            android:hint="Jewellery Name"
                            android:textColor="#000000"
                            android:background="@drawable/Black"
                            android:textSize="16dp" />

                    <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content">
                        <Spinner
                                android:id="@+id/Jtype"
                                android:layout_width="0dp"
                                android:layout_weight="1"
                                android:layout_height="wrap_content"
                                android:text="Jewellery Type"
                                android:background="@drawable/Black"
                                android:textColor="#FFFFFF"
                                android:textSize="18dp" />

                        <Spinner
                                android:id="@+id/weight"
                                android:layout_width="0dp"
                                android:layout_weight="1"
                                android:layout_height="wrap_content"
                                android:hint="Karat"
                                android:textColor="#FFFFFF"
                                android:background="@drawable/Black"
                                android:background="@drawable/Black"
                                android:textSize="18dp" />
                    </LinearLayout>

                    <EditText
                            android:id="@+id/JWeight"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:ems="10"
                            android:hint="Enter Gold Weight (gms)"
                            android:textColor="#000000"
                            android:textSize="16dp"/>
                </LinearLayout>

            </LinearLayout>
        </LinearLayout>

    </ScrollView>

</LinearLayout>
于 2013-09-25T05:52:42.813 回答