简单的Android表格格式化自己的小部件
从历史上看,我在布局上有愚蠢的问题,现在我试图填补我大脑中的空白。看这个例子,我试着做一些轴的图形,我想打包如下: 左侧垂直线 底部水平线 左下角垂直和水平之间的垫片 用图形填充其他位置 (很抱歉,不允许发布图像。我希望您在之前看到图形轴,无需解释。) 这就是我现在得到的:简单的Android表格格式化自己的小部件,android,layout,formatting,Android,Layout,Formatting,从历史上看,我在布局上有愚蠢的问题,现在我试图填补我大脑中的空白。看这个例子,我试着做一些轴的图形,我想打包如下: 左侧垂直线 底部水平线 左下角垂直和水平之间的垫片 用图形填充其他位置 (很抱歉,不允许发布图像。我希望您在之前看到图形轴,无需解释。) 这就是我现在得到的: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.example.graphicformatting.Axis
android:id="@+id/yaxis"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="40dp"
android:background="@color/backpanel_dark"
custom:ishorizontal="false" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textMultiLine"
android:text="This text field mean my own graphic widget, which not have any OnMeasured function or any specific and it's painting with size, which assigment by parent/ Just use getHeight and getWeight" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<View
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@color/backpanel_dark" />
<com.example.graphicformatting.Axis
android:id="@+id/yaxis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:background="@color/backpanel_dark"
custom:ishorizontal="true" />
</LinearLayout>
这是我的Axis的代码:
package com.example.graphicformatting;
public class Axis extends View {
public static final int HORIZONTAL = 0;
public static final int VERTICAL = 1;
public static final int DP_SIZE = 40;
int type = -1;
int size;
Paint painter;
public Axis(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.com_example_graphicformatting_Axis);
final int N = a.getIndexCount();
for (int i = 0; i < N; ++i) {
int attr = a.getIndex(i);
switch (attr) {
case R.styleable.com_example_graphicformatting_Axis_ishorizontal:
if (a.getBoolean(attr, true)) {
type = HORIZONTAL;
} else
type = VERTICAL;
break;
default:
Log.d("axis", "Found unexpected state");
}
}
a.recycle();
final float scale = getContext().getResources().getDisplayMetrics().density;
size = (int) (DP_SIZE * scale + 0.5f);
painter = new Paint();
painter.setColor(Color.RED);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
if (type == HORIZONTAL) {
this.setMeasuredDimension(parentWidth, size);
this.setLayoutParams(new LinearLayout.LayoutParams(parentWidth, size));
// Log.d("axis", "Size of horizontal calc: " + (parentWidth) + " "
// + size);
} else if (type == VERTICAL) {
this.setMeasuredDimension(size, parentHeight);
this.setLayoutParams(new LinearLayout.LayoutParams(size, parentHeight));
// Log.d("axis", "Size of vertical calc: " + size + " "
// + (parentHeight));
} else { // Для нормального отображения в конструкторе
this.setMeasuredDimension(size, size);
this.setLayoutParams(new LinearLayout.LayoutParams(size, size));
}
}
@Override
protected void onDraw(Canvas canvas) {
if (type == VERTICAL) {
canvas.drawRect(0, 0, size, getHeight(), painter);
} else {
canvas.drawRect(0, getHeight() - size, getWidth(), getHeight(), painter);
}
super.onDraw(canvas);
}
package com.example.graphicsformatting;
公共类Axis扩展了视图{
公共静态最终int水平=0;
公共静态最终int垂直=1;
公共静态最终int DP_尺寸=40;
int type=-1;
整数大小;
油漆工;
公共轴(上下文、属性集属性){
超级(上下文,attrs);
TypedArray a=上下文。获取样式属性(属性,
R.styleable.com_示例_图形格式_轴);
final int N=a.getIndexCount();
对于(int i=0;i
}
如何自定义我的布局以获得正确的图片?
Ou,这将用于设置水平/垂直属性(value/attrs.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="com.example.graphicformatting.Axis">
<attr name="ishorizontal" format="boolean"></attr>
</declare-styleable>
</resources>