Android:如何绘制一个LinearLayout的边框

我有三个文件。 XML,绘图function和主要活动。 我在我的XML文件中有一些LinearLayout

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

这是绘图function:

 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时,程序崩溃。

另一件事,我想在LinearLayout的中心绘制两个圆,但是我怎么能找出中心坐标?

你真的需要这样做吗?

只是考虑标题:你可以使用一个ShapeDrawable作为android:背景…

例如,让我们将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> 

并定义android:background =“@ drawable / my_custom_background”。

我没有testing,但它应该工作。

更新:

如果这符合你的需求,我认为最好利用xmlforms的可绘制资源。 用“from scratch”项目(对于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> 

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 shape / stroke的值相关联。 请不要在你的最终应用中使用@android:color/white ,而应该使用项目定义的颜色。

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

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

你的问题在使用getBorder.set{Width,Height}(100); 。 为什么你在onClick方法中做到这一点?

我需要进一步的信息,不要错过这一点:你为什么这样做呢? 你需要一个dynamic的行为? 你input的drawable是png还是ShapeDrawable是可以接受的? 等等

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

扩展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) ; } } 

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