android中的布局设置

时间:2012-07-19 15:45:43

标签: android android-layout

我有以下结构:

<LinearLayout
    android:id="@+id/linearLayout5"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:background="@drawable/framewhite"
    android:orientation="vertical" 
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_weight="1"
    android:gravity="right"
    >


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        >

       <TextView
            android:id="@+id/txtBestPickTrivia"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/testTrivia"
            android:textAppearance="?android:attr/textAppearanceMedium" />


    </ScrollView>

    <TextView
        android:id="@+id/txtEntityName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        android:layout_marginBottom="10dp"
        android:layout_marginRight="20dp"
        android:text="Cars (2006)" />

</LinearLayout>

问题出现在ScrollView的这一行:

机器人:layout_height = “120dp”

我希望根据屏幕分辨率调整高度,我使用 3.7in WVGA(Nexus One)进行设计,这在我的HTC Desire中工作正常但是当我测试了我的时候使用HTC Explorer的应用程序使用 3.2 HVGA 它不起作用,ScrollView穿过屏幕边界,我看不到我的TextView即txtEntityName。

还有一件事,是否有任何方法可以停靠任何控件,如在.NET中控件可以停靠在顶部,底部,右侧或左侧?

4 个答案:

答案 0 :(得分:1)

试试这段代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout5"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:background="@drawable/framewhite"
    android:orientation="vertical" 
    android:gravity="right">

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:layout_weight="1" >

        <TextView
            android:id="@+id/txtBestPickTrivia"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/framewhite"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </ScrollView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right" >

        <TextView
            android:id="@+id/txtEntityName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="2dp"
            android:text="Cars (2006)" />
    </LinearLayout>

</LinearLayout>

它必须适用于所有Android屏幕尺寸

答案 1 :(得分:0)

答案 2 :(得分:0)

对于“对接”,您可以使用android:gravity和/或绝对对齐属性(即alignParentTop)

答案 3 :(得分:0)

您应该使用RelativeLayout而不是LinearLayout,并将txtEntityName对齐到底部,将ScrollView对齐到父级的顶部,将其底部对齐到txtEntityName的顶部。将ScrollView的layout_height设置为0dp