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

查看:223
本文介绍了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);
    }
}

这是主要的活动。

And this is the main Activity.

 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,程序粉碎。有人能帮助我吗?另一件事,我要画两个圈中的LinearLayout的中心,但我怎么能计算出中心坐标?

The program could draw border but the size didn't fit the LinearLayout. And when I click the LinearLayout again, the program crushed. Could someone help me? Another thing, I want 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作为机器人:背景...

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

例如,让我们定义 RES /绘制/ my_custom_background.xml 为:

<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:背景=@可绘制/ my_custom_background

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

我没有测试过,但它应该工作。

I've not tested but it should work.

更新:

我觉得这是更好地利用XML形状绘制资源的权力,如果适合您的需要。着有从头开始的项目(为Android-8),定义RES /布局/ 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 /绘制/ 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:彩色/白色在最终的应用程序,而是一个定义的项目颜色

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.

您可以使用安卓背景=@可绘制/边框机器人:填充=10dip到每个的LinearLayout从您提供的样本

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

至于你显示一些圈子里的LinearLayout的背景,我在和插图/缩放/层绘制资源(与其他职位的see可绘制资源获得更多信息)得到的东西的工作在一个的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 {宽度,高度}(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.

要继续(也许明天,只要你提供更多的precisions你想达到什么)......

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

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

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