【问题标题】:Android view customization problemAndroid视图自定义问题
【发布时间】:2011-06-10 08:29:15
【问题描述】:

我正在尝试使用以下视图小部件组织我的视图 -

  • TextView(应填满屏幕宽度并具有特定高度)
  • 使用 Draw() 绘制的自定义视图(填充宽度,应该在上面的 textview 之后开始并填充屏幕)

我正在尝试编写一个棋盘游戏,因此绘制了自定义视图。

我的问题是什么是最好的布局?

我可以使用 TableLayout 并将这两个视图分别作为一列吗?我认为这可以解决问题,但我用于自定义视图的任何布局都不会填满屏幕,即使我正在绘制它的 tablerow 填满了屏幕。

我希望我能够正确地解释自己。任何指针都非常感谢。

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/root_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/tl_question"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:shrinkColumns="1"
            android:stretchColumns="2">

            <TableRow>
                <TextView
                    android:id="@+id/question"
                    android:layout_width="wrap_content"
                    android:textSize="30sp"
                    android:textColor="#ffFFFFFF"   
                    android:layout_gravity="center_vertical"
                    android:paddingBottom="9dip"
                    android:layout_marginLeft="3dip"
                    android:layout_marginRight="2dip"/>
            </TableRow>

            <TableRow>
                <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_gravity="center_vertical" >
                    <com.ac.gui.CustomView
                        android:id="@+id/custom_board"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:keepScreenOn="true" />
                </LinearLayout>
            </TableRow>
        </TableLayout>

</RelativeLayout>

【问题讨论】:

    标签: android android-layout android-ui


    【解决方案1】:

    试试这个

    <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/root_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
    
                <TextView
                    android:id="@+id/question"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:textSize="30sp"
                    android:text="ASDFASDF"
                    android:textColor="#ffFFFFFF"   
                    android:layout_gravity="center_vertical"
                    android:paddingBottom="9dip"
                    android:layout_marginLeft="3dip"
                    android:layout_marginRight="2dip"/>
    
               <com.ac.gui.CustomView
    
                android:layout_below="@+id/question"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" />
    
    </RelativeLayout>
    

    我测试了一个不同的布局来代替你的布局,它工作正常。它也应该适用于您的。

    RelativeLayout 允许您相对于彼此定位项目。 您会注意到自定义视图上的属性“layout_below”,我在其中指定了要放置它的视图的 ID。

    【讨论】:

    • 感谢您的回复。这很有意义。我想多了。如果我想在自定义视图下方添加另一个文本视图或按钮怎么办?理想情况下,我不想修复自定义视图的高度。所以我希望它相对于它下面的文本视图的高度。能做到吗?
    • 当然!您想要做的是将 TextView 放在 XML 中的自定义视图之上。确保给 TextView 一个 Id。然后,在您的 CustomView 中,添加属性 android:layout_above="@id/whateveryounamedit"。现在您的自定义视图将显示在问题文本视图下方和新文本视图上方! :) 您应该将 TextView 放在自定义视图之前,因为 XML 是内联读取的。因此,如果您将 layout_above 属性放在 CustomView 上并在其下方放置 TextView,它就不会知道您正在引用的资源。(这可能已在较新的 Eclipse 插件中得到修复)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 2014-10-31
    • 1970-01-01
    相关资源
    最近更新 更多