Android:如何为 LinearLayout 绘制边框 [英] Android: how to draw a border to a LinearLayout

查看:134
本文介绍了Android:如何为 LinearLayout 绘制边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个文件.XML、绘图功能和主要活动.我的 XML 文件中有一些 LinearLayout.

I have three files. The XML, the draw function and the main Activity. I have some LinearLayout in my XML file.

<LinearLayout android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_weight="1">
    <LinearLayout android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:background="#ef3"
                  android:id="@+id/img01"/>
    <LinearLayout android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:layout_weight="1"
                  android:background="#E8A2B4"
                  android:id="@+id/img02"/>
</LinearLayout>

这是绘制函数:

public class getBorder extends TextView {
    public getBorder(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();

        paint.setColor(android.graphics.Color.RED);

        canvas.drawLine(0, 0, this.getWidth() - 1, 0, paint);
        canvas.drawLine(0, 0, 0, this.getHeight() - 1, paint);
        canvas.drawLine(this.getWidth() - 1, 0, this.getWidth() - 1,
            this.getHeight() - 1, paint);
        canvas.drawLine(0, this.getHeight() - 1, this.getWidth() - 1,
            this.getHeight() - 1, paint);
    }
}

这是主要的活动:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final getBorder getBorder = new getBorder(this);
    final LinearLayout img01 = (LinearLayout) findViewById(R.id.img01);
    img01.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            getBorder.setWidth(100);
            getBorder.setHeight(100);
            img01.addView(getBorder);
        }
    });       
}

程序可以绘制边框,但尺寸不适合LinearLayout.当我再次单击 LinearLayout 时,程序崩溃了.

The program could draw border but the size doesn't fit the LinearLayout. And when I click the LinearLayout again, the program crashed.

另外,我想在LinearLayout的中心画两个圆,但是我怎么知道中心坐标?

Also, I want to draw two circles in the center of the LinearLayout, but how could I figure out the center coordinates?

推荐答案

您真的需要以编程方式执行此操作吗?

Do you really need to do that programmatically?

仅考虑标题:您可以使用 ShapeDrawable 作为 android:background...

Just considering the title: You could use a ShapeDrawable as android:background…

例如,让我们定义 res/drawable/my_custom_background.xml 为:

For example, let's define res/drawable/my_custom_background.xml as:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
  <corners
      android:radius="2dp"
      android:topRightRadius="0dp"
      android:bottomRightRadius="0dp"
      android:bottomLeftRadius="0dp" />
  <stroke
      android:width="1dp"
      android:color="@android:color/white" />
</shape>

并定义 android:background="@drawable/my_custom_background".

and define android:background="@drawable/my_custom_background".

我没有测试过,但它应该可以工作.

I've not tested but it should work.

更新:

我认为如果满足您的需要,最好利用 xml shape drawable 资源功能.使用从头开始"项目(针对 android-8),定义 res/layout/main.xml

I think that's better to leverage the xml shape drawable resource power if that fits your needs. With a "from scratch" project (for android-8), define res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/border"
    android:padding="10dip" >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World, SOnich"
        />
    [... more TextView ...]
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World, SOnich"
        />
</LinearLayout>

和一个 res/drawable/border.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
   <stroke
        android:width="5dip"
        android:color="@android:color/white" />
</shape>

据报道在姜饼设备上工作.请注意,您需要将 LinearLayout 的 android:paddingandroid:width 形状/笔划的值相关联.请不要在最终应用程序中使用 @android:color/white,而是使用项目定义的颜色.

Reported to work on a gingerbread device. Note that you'll need to relate android:padding of the LinearLayout to the android:width shape/stroke's value. Please, do not use @android:color/white in your final application but rather a project defined color.

您可以将 android:background="@drawable/border" android:padding="10dip" 应用到您提供的示例中的每个 LinearLayout.

You could apply android:background="@drawable/border" android:padding="10dip" to each of the LinearLayout from your provided sample.

至于您的其他与将一些圆圈显示为 LinearLayout 的背景相关的帖子,我正在使用 Inset/Scale/Layer 可绘制资源(参见 Drawable Resources 以获取更多信息)以获得一些工作以在 LinearLayout 的背景中显示完美的圆形但目前失败......

As for your other posts related to display some circles as LinearLayout's background, I'm playing with Inset/Scale/Layer drawable resources (see Drawable Resources for further information) to get something working to display perfect circles in the background of a LinearLayout but failed at the moment…

您的问题显然在于 getBorder.set{Width,Height}(100); 的使用.为什么要在 onClick 方法中执行此操作?

Your problem resides clearly in the use of getBorder.set{Width,Height}(100);. Why do you do that in an onClick method?

我需要更多信息以免错过重点:为什么要以编程方式执行此操作?你需要动态行为吗?您的输入可绘制对象是 png 或 ShapeDrawable 是否可以接受?等

I need further information to not miss the point: why do you do that programmatically? Do you need a dynamic behavior? Your input drawables are png or ShapeDrawable is acceptable? etc.

有待继续(也许明天,一旦您提供更精确的目标)......

To be continued (maybe tomorrow and as soon as you provide more precisions on what you want to achieve)…

这篇关于Android:如何为 LinearLayout 绘制边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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