我正在开发一个为DB创建UML图的项目。问题是我很难实现实际的类对象(包含类名,pk,attribs等的对象)。是否可以创建一个扩展LinearLayout的类,同时在里面包含3个TextView来显示数据?我需要它在点击按钮时动态创建。谢谢!
答案 0 :(得分:0)
当然有可能。 Android为您提供了很多创建自己的视图和视图组的能力。 Here training你可以从那里开始。
以下是LinearLayout
内部有三个TextView
的一些非常基本的示例:
<强> MyActivity.java 强>
public class MyActivity extends Activity implements View.OnClickListener {
private static final String TAG = "MyActivity";
private Button mCreateViewButton = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mCreateViewButton = (Button) findViewById(R.id.createView);
mCreateViewButton.setOnClickListener(this);
}
@Override
public void onClick(final View view) {
switch(view.getId()) {
case R.id.createView:
// Add new class view
final ClassItemView item = new ClassItemView(this);
final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT, 1);
item.setClassName("MyClass").setMethods("myMethod()").setAttributes("int myAttribute");
((RelativeLayout)findViewById(R.id.mainContainer)).addView(item, params);
break;
}
}
}
<强> main.xml中强>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainContainer"
android:background="#fff3f2f2"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/createView"
android:text="Create text view"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="5dp"/>
</RelativeLayout>
<强> ClassItemView.java 强>
public class ClassItemView extends LinearLayout {
private TextView mClassNameView;
private TextView mMethodsView;
private TextView mAttributesView;
private Context mContext;
/**
* Default constructor
*
* @param context {@link android.content.Context}
*/
public ClassItemView(@NotNull final Context context) {
super(context);
setOrientation(VERTICAL);
mContext = context;
initChild();
setupBorder();
}
/**
* Setups border for linear layout
*/
private void setupBorder() {
setBackground(mContext.getResources().getDrawable(R.drawable.border_bg));
setDividerDrawable(mContext.getResources().getDrawable(android.R.drawable.divider_horizontal_bright));
setShowDividers(SHOW_DIVIDER_MIDDLE);
}
/**
* Inits child views
*/
private void initChild() {
final int padding = getResources().getDimensionPixelSize(R.dimen.padding);
mClassNameView = new TextView(mContext);
mClassNameView.setTypeface(null, Typeface.BOLD);
// Set paddings for border, not needed if bg is 9png
mClassNameView.setPadding(padding, padding, padding, padding);
// Setup other properties like font size etc.
mMethodsView = new TextView(mContext);
// Set paddings for border, not needed if bg is 9png
mMethodsView.setPadding(padding, padding, padding, padding);
mAttributesView = new TextView(mContext);
// Set paddings for border, not needed if bg is 9png
mAttributesView.setPadding(padding, padding, padding, padding);
final LinearLayout.LayoutParams classNameParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
addView(mClassNameView, classNameParams);
final LinearLayout.LayoutParams methodsParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
addView(mMethodsView, methodsParams);
final LinearLayout.LayoutParams attribParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
addView(mAttributesView, attribParams);
}
/**
* @param name class name
*/
public ClassItemView setClassName(final CharSequence name) {
mClassNameView.setText(name);
return this;
}
/**
* @param methods methods string
*/
public ClassItemView setMethods(final CharSequence methods) {
mMethodsView.setText(methods);
return this;
}
/**
* @param attrs methods string
*/
public ClassItemView setAttributes(final CharSequence attrs) {
mAttributesView.setText(attrs);
return this;
}
}
<强> border_bg.xml 强>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke android:color="#0F0F0F" android:width="2dp"/>
</shape>
</item>
</layer-list>
结果如下所示: