如何将一些XML元素包含在边界框中?

时间:2014-05-20 21:13:35

标签: xml android-layout android-xml rectangles bounding-box

我想在这里附上一对复选框和单选按钮:

enter image description here

...在一个矩形或"边界框"所以它看起来像这样:

enter image description here

......但当然不那么难看。我如何使用以下XML作为起点:

<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="match_parent"
    android:orientation="vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="hhs.app.SettingsActivity">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dip"
        android:text="@string/select_your_printer"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <CheckBox
        android:id="@+id/ckbxNoPrinter"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/no_printer" />

    <CheckBox
        android:id="@+id/ckbxZebraQL220"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@string/zebra_ql220" />

    <Space
        android:layout_width="match_parent"
        android:layout_height="5dp" />

    . . .

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RadioButton
            android:id="@+id/radbtnBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/using_labels_black" />

        <RadioButton
            android:id="@+id/radbtnPlain"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/using_plain_labels" />
    </RadioGroup>

    <Space
        android:layout_width="match_parent"
        android:layout_height="2dp" />

    . . .
    </LinearLayout>

</LinearLayout>

更新

我尝试应用Der Golem的建议,如下:

<LinearLayout 
    android:orientation="vertical"
    android:layout_width="wrap_content">
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle"
        android:layout_width="wrap_content">
        <stroke
            android:width="4dp"
            android:color="#f000"
            android:layout_width="wrap_content" />
        <TextView
            android:text="@string/after_fetching_palabra"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            />
    </shape>
</LinearLayout>

......但这没有任何作用(好); TextView就在那里作为测试,从一个简单的小部件开始。

注意:这是LinearLayout中的LinearLayout。当我删除它时,它很好,但有了它,布局甚至不会渲染。

更新2

所以我认为我将形状放在\ res \ drawable文件夹中。但在Droidio中,它为我提供了四个,数量为四个,可绘制的文件夹,即:

drawable-hdpi
drawable-mdpi
drawable-xhdpi
drawable-xxhdpi

我是否需要将xml放在这些文件夹的每个中,或者我可以创建&#34; generic / default&#34;普通的老&#34; drawable&#34;文件夹并把它放在那里?

1 个答案:

答案 0 :(得分:3)

使用垂直LinearLayout来封装你的CheckBoxes和另一个封装你的RadioButtons,并为它们分配一个xml drawable作为背景(如果你觉得更舒服,你也可以使用9补丁):

另外两个LinearLayouts是外部容器的子项。

/res/drawable/box.xml(自包含的xml drawable):

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    >
    <stroke
        android:width="4dp"
        android:color="#f000"
    />
</shape>

供参考:http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape

<强> [编辑]

将xml drawable应用为普通背景:

这只是两个人中的一个:&#34; CheckBox&#34;之一:

...

<LinearLayout 
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/box"
    >
     <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dip"
        android:text="@string/select_your_printer"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <CheckBox
        android:id="@+id/ckbxNoPrinter"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/no_printer" />

    <CheckBox
        android:id="@+id/ckbxZebraQL220"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="@string/zebra_ql220" />
</LinearLayout>

...

[编辑2]

现在有点离题了 你问了一个简单的方框,没有别的,这就是我提供的东西 但。在我提供的链接中详细显示了如何(例如)不仅使角落成圆角,而且还为您的形状提供不同的背景颜色甚至< strong>颜色渐变。
只是为了描述Shape Drawable的一些有趣的属性。