【问题标题】:Create border around a centered custom view and removing useless LinearLayout?在居中的自定义视图周围创建边框并删除无用的 LinearLayout?
【发布时间】:2012-08-14 12:26:02
【问题描述】:

目前我有以下布局:

<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 parent是没用的; 将背景属性转移到另一个视图。

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

还有其他方法可以在 EditorView 之外添加边框还是应该忽略警告?

问候 异种狼疮

【问题讨论】:

    标签: android xml android-layout view border


    【解决方案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"
    

    【讨论】:

      【解决方案2】:

      View 默认可以绘制背景。如果你正确地开发了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()

      【讨论】:

        猜你喜欢
        • 2011-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多