我有三个文件。在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的背景,我在和插图/缩放/层绘制资源(与其他职位的