我怎样才能括在边框部分的XML元素? [英] How can I enclose some XML elements in a bounding box?

查看:320
本文介绍了我怎样才能括在边框部分的XML元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要附上对复选框和这里的单选按钮:

I want to enclose the pair of checkboxes and the radio buttons here:

...在矩形或边框,使之看起来是这样的:

...in a rectangle or "bounding box" so that it look something like this:

...但不太难看的,当然。我该怎么做,与下面的XML作为一个起点:

...but less ugly, of course. How can I do that with the following XML as a starting point:

<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>

我想申请明镜魔像的建议,像这样:

I tried to apply Der Golem's suggestion, like so:

<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的只是在那里作为一个测试,用一个简单的小部件开始,开始使用。

...but that does nothing (good); the TextView is just in there as a test, starting with one simple widget to begin with.

请注意:这是一个的LinearLayout内的LinearLayout。当我删除此,这很好,但有了它,布局甚至不会渲染。

NOTE: This is a LinearLayout within a LinearLayout. When I remove this, it's fine, but with it, the Layout won't even render.

所以我认为,我把形状在\\资源\\文件夹绘制。但在Droidio,它给了我四,算上他们,四,绘制文件夹,分别是:

So I take it that I put the shape in a \res\drawable folder. But in Droidio, it gives me four, count 'em, four, drawable folders, namely:

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

我需要把XML中的每个的这些文件夹中,或者我可以创造一个通用/默认的纯老绘制文件夹,把它放在那里?

Do I need to put the xml in each of those folders, or can I create a "generic/default" plain old "drawable" folder and put it there?

推荐答案

使用一个垂直的LinearLayout附上您的复选框和另一个附上您的单选按钮,并为它们分配一个XML绘为背景(你可以选择使用一个9补丁,如果你觉得更舒服):

Use a vertical LinearLayout to enclose your CheckBoxes and another one to enclose your RadioButtons and assign them an xml drawable as the background (you could alternatively use a 9 patch, if you feel more comfortable with):

这两个额外的LinearLayouts是外容器的儿童

The two extra LinearLayouts are children of the outer container.

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

/res/drawable/box.xml (self contained 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绘制作为一个正常的背景:

Apply the xml drawable as a normal background:

这仅仅是两个layous之一:勾选之一:

This is just one of the two layous: the "CheckBox" one:

...

<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>

...

现在一点题外话。结果
你问一个简单的方盒子,没有别的,所以这是我提供的东西。结果
但。在链接我提供详细的展示了如何可以(例如)不仅使边角圆,但也给你的形状不同的背景颜色或甚至< STRONG>颜色渐变。结果
仅举形状绘制对象的一些有趣的属性。

Now a little digression.
You asked for a simple square box and nothing else, so that's what I provided.
BUT. In the link I provided is shown in details how you can (for instance) not only make the corners round, but also give your shape a different background color or even a color gradient.
Just to name some interesting properties of the Shape Drawable.

这篇关于我怎样才能括在边框部分的XML元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆