【问题标题】:Dynamically create class that extends LinearLayout and contains TextView inside动态创建扩展 LinearLayout 并在内部包含 TextView 的类
【发布时间】:2014-03-18 17:09:52
【问题描述】:

我正在开发一个为 DB 创建 UML 图的项目。问题是我很难实现实际的类对象(包含类名、pk、属性等的对象)。是否可以创建一个扩展 LinearLayout 的类,同时在内部包含 3 个 TextView 来显示数据?我需要它在按钮的单击上动态创建。谢谢!

【问题讨论】:

    标签: android class textview uml android-linearlayout


    【解决方案1】:

    当然可以。 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;
            }
        }
    }
    

    ma​​in.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>
    

    结果将如下所示:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-15
      相关资源
      最近更新 更多