Android:如何将边框绘制为LinearLayout

SPG*_*SPG 186 android draw android-layout

我有三个文件.XML,绘图函数和主Activity.LinearLayout我的XML文件中有一些.

<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>
Run Code Online (Sandbox Code Playgroud)

这是绘图功能:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是主要的活动:

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);
        }
    });       
}
Run Code Online (Sandbox Code Playgroud)

该计划可以绘制边界,但大小不适合LinearLayout.当我LinearLayout再次点击时,程序崩溃了.

另外,我想在中心画两个圆圈LinearLayout,但我怎么能找出中心坐标呢?

Ren*_*aud 433

你真的需要以编程方式做到这一点吗?

只考虑标题:你可以使用ShapeDrawable作为android:background ...

例如,让我们定义res/drawable/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>
Run Code Online (Sandbox Code Playgroud)

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

我没有测试但它应该工作.

更新:

我认为如果符合您的需求,最好利用xml形状的可绘制资源.使用"从头开始"项目(对于android-8),定义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>
Run Code Online (Sandbox Code Playgroud)

和a 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>
Run Code Online (Sandbox Code Playgroud)

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

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

至于你的其他帖子有关将一些圆圈显示为LinearLayout的背景,我正在使用Inset/Scale/Layer可绘制资源(请参阅Drawable Resources以获取更多信息),以便在LinearLayout的背景中显示完美的圆形但是失败了在这一刻…

你的问题明显存在于使用中getBorder.set{Width,Height}(100);.为什么在onClick方法中这样做?

我需要进一步的信息,不要错过这一点:为什么你这样编程?你需要动态行为吗?您的输入drawable是png还是ShapeDrawable可以接受?等等

继续(也许是明天,一旦你为你想要达到的目标提供更多的精确度)......

  • 不确定为什么会这样,但当我在`LinearLayout`上使用它时,我从边框颜色得到了一个实心填充,除非我将以下子元素添加到`shape`元素:`<solid android:color ="@ android:color/transparent"/>` (12认同)
  • 对我而言,我必须添加一个,`<solid android:color ="@ color/lighter_gray"/>`否则我得到一个黑色的背景 (2认同)

Gab*_*iba 16

扩展LinearLayout/RelativeLayout并在XML上直接使用它

package com.pkg_name ;
...imports...
public class LinearLayoutOutlined extends LinearLayout {
    Paint paint;    

    public LinearLayoutOutlined(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        setWillNotDraw(false) ;
        paint = new Paint();
    }
    public LinearLayoutOutlined(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        setWillNotDraw(false) ;
        paint = new Paint();
    }
    @Override
    protected void onDraw(Canvas canvas) {
        /*
        Paint fillPaint = paint;
        fillPaint.setARGB(255, 0, 255, 0);
        fillPaint.setStyle(Paint.Style.FILL);
        canvas.drawPaint(fillPaint) ;
        */

        Paint strokePaint = paint;
        strokePaint.setARGB(255, 255, 0, 0);
        strokePaint.setStyle(Paint.Style.STROKE);
        strokePaint.setStrokeWidth(2);  
        Rect r = canvas.getClipBounds() ;
        Rect outline = new Rect( 1,1,r.right-1, r.bottom-1) ;
        canvas.drawRect(outline, strokePaint) ;
    }

}
Run Code Online (Sandbox Code Playgroud)
<?xml version="1.0" encoding="utf-8"?>

<com.pkg_name.LinearLayoutOutlined
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
    android:layout_width=...
    android:layout_height=...
   >
   ... your widgets here ...

</com.pkg_name.LinearLayoutOutlined>
Run Code Online (Sandbox Code Playgroud)

  • 请不要在`onDraw()`方法中分配内存,在`init()`方法中创建对象,由构造函数调用并在`onDraw()`方法中重用它们.分配在`onDraw()`(每秒调用60次)会导致性能不佳,电池耗尽等 (31认同)