在居中的自定义视图周围创建边框并删除无用的LinearLayout?

时间:2012-08-14 12:26:02

标签: android xml android-layout view border

目前我有以下布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/editorRootView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout android:id="RL1"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1">
        <!-- LinearLayout needed so we have an border outside of the EditorView -->
        <LinearLayout android:id="LL1"
            android:background="@drawable/border"
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <xenolupus.EditorView
                android:id="@+id/editorView"
                android:layout_width="300dip"
                android:layout_height="216dip" />
        </LinearLayout>
    </RelativeLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/otherImage"
            android:layout_width="150dip"
            android:layout_height="60dip"
            android:text="@string/cardEditor_OtherImageButtonText" />
        <Button
            android:id="@+id/next"
            android:layout_width="150dip"
            android:layout_height="60dip"
            android:text="@string/cardEditor_NextButtonText" />
    </LinearLayout>
</LinearLayout>

使用的@ drawable / border:

<?xml version="1.0" encoding="UTF-8"?>
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid 
        android:color="#FFFFFF" />
    <stroke 
        android:width="10dip" 
        android:color="#FF0099CA" />
    <padding 
        android:left="10dip" 
        android:top="10dip" 
        android:right="10dip"
        android:bottom="10dip" />
    <corners 
        android:radius="5dip" />
</shape>

然而Eclipse警告我(带有!的黄色三角形),RelativeLayout RL1中的LinearLayout LL1是无用的,应该删除:

  

此LinearLayout布局或其RelativeLayout父布局无用;   将background属性传递给另一个视图。

由于需要RelativeLayout来使EditorView居中,我尝试删除LinearLayout LL1并将LinearLayout LL1的android:background添加到EditorView。但是,让边框消失在EditorView的内容后面。

是否有另一种方法可以在EditorView之外添加边框,还是应该忽略警告?

问候 Xeno Lupus

2 个答案:

答案 0 :(得分:1)

    yes it's right, put the background inside your <xenolupus.EditorView 
        like this    
<xenolupus.EditorView
                    android:background="@drawable/border"
                    android:padding="10dp"
                    android:id="@+id/editorView"
                    android:layout_width="300dip"
                    android:layout_height="216dip" />

    and then  add gravity to it parent the RL1 layout  

    > android:gravity="center"

答案 1 :(得分:0)

视图默认情况下可以绘制背景。如果您正确地开发了EditorView,则可以直接在XML中设置背景和填充:

<xenolupus.EditorView
    android:id="@+id/editorView"
    android:layout_width="300dip"
    android:layout_height="216dip"
    android:background="@drawable/border"
    android:padding="10dp" />

“正确开发”我的意思是您的视图计算它的高度宽度(onMeasureonSizeChanged方法) 填充。换句话说:在计算大小时,您使用了getPadding*()方法。

注意

不要忘记致电super.onDraw()