繁体   English   中英

自定义视图未显示在活动中

[英]Custom view not displaying in activity

我有一个自定义控件,它应该像iOS中的Segmenttab控制器一样工作。

它具有3个textview,其布局如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/groupofthree"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/background"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tv_1"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="@color/black"
        android:gravity="center"
        android:text="Retiree"
        android:textColor="@color/whitetext"
        android:textSize="15dp"
        android:textStyle="normal" />

    <TextView
        android:id="@+id/tv_2"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="@color/lightGrey"
        android:gravity="center"
        android:padding="3dp"
        android:text="Under18/fulltime"
        android:textColor="@color/whitetext"
        android:textSize="15dp"
        android:textStyle="normal" />

    <TextView
        android:id="@+id/tv_3"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="@color/black"
        android:gravity="center"
        android:text="Others"
        android:textColor="@color/whitetext"
        android:textSize="15dp"
        android:textStyle="normal" />

</LinearLayout>

该控件如下所示:

    public class SegmentedRadioGroup extends View{

        private Context m_Context;

        public TextView tv1;
        public TextView tv2;
        public TextView tv3;
        int selectedIndex = 0;

        public SegmentedRadioGroup(Context context, AttributeSet attrs) {

            super(context, attrs);
            m_Context = context;

        }

        public SegmentedRadioGroup(Context context, AttributeSet attrs, int defStyle) {

            super(context, attrs, defStyle);
        }

        public SegmentedRadioGroup(Context context) {
            super(context);
            // TODO Auto-generated constructor stub

            m_Context = context;
            LayoutInflater inflater = (LayoutInflater) m_Context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = inflater.inflate(R.layout.threeradiobutton, null);


            v.isInEditMode();
            tv1 = (TextView) v.findViewById(R.id.tv_1);
            tv2 = (TextView) v.findViewById(R.id.tv_2);
            tv3 = (TextView) v.findViewById(R.id.tv_3);

            tv1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    selectedIndex = 0;
                    tv1.setBackgroundColor(R.color.darkgrey);
                    tv2.setBackgroundColor(R.color.lightGrey);
                    tv3.setBackgroundColor(R.color.lightGrey);
                }
            });

            tv2.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    selectedIndex = 1;
                    tv1.setBackgroundColor(R.color.lightGrey);
                    tv2.setBackgroundColor(R.color.darkgrey);
                    tv3.setBackgroundColor(R.color.lightGrey);

                }
            });

            tv3.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    selectedIndex = 2;

                    tv1.setBackgroundColor(R.color.lightGrey);
                    tv2.setBackgroundColor(R.color.lightGrey);
                    tv3.setBackgroundColor(R.color.darkgrey);
                }
            });
        }

        @Override
        protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub
            super.onDraw(canvas);


        }


    }

我将自定义视图添加到布局的活动如下:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/background"
        android:orientation="vertical" >

        <RelativeLayout
            android:id="@+id/widget1216"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/appspecific_menubar" >

            <TextView
                android:id="@+id/widget1222"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:text="@string/transaction_accounts_topbar"
                android:textColor="@color/whitetext"
                android:textSize="18sp"
                android:textStyle="bold" >
            </TextView>

            <ImageButton
                android:id="@+id/home_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:background="@null"
                android:paddingBottom="5dip"
                android:paddingLeft="10dip"
                android:paddingTop="5dip"
                android:src="@drawable/home" >
            </ImageButton>
        </RelativeLayout>
    <LinearLayout 
        android:id="@+id/testLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" />.
     </LinearLayout>

活动看起来像这样。 我已经使用addview将视图添加到布局中。

    public class TransactionAccount extends Activity {
        LinearLayout selector;
        SegmentedRadioGroup sg_test;
        LayoutInflater inflater;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.transactionaccount_main);
            selector = (LinearLayout)findViewById(R.id.testLayout);




            sg_test = new SegmentedRadioGroup(this);
            selector.addView(sg_test);



    //      inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    //      selector = inflater.inflate(R.id., null);

    //      sg_test = new SegmentedRadioGroup(this);

    //      sg_test.tv1.setText("1");
    //      sg_test.tv2.setText("2");
    //      sg_test.tv3.setText("3");
    //      sg_test.tv1.setBackgroundColor(R.color.blacktext);
    //      sg_test.setVisibility(View.VISIBLE);
    //      
    //      Log.d("TransactionAccount", "onCreate++++++" + sg_test.tv1.getText());
    //      Log.d("TransactionAccount", "onCreate++++++" + sg_test.tv2.getText());
    //      Log.d("TransactionAccount", "onCreate++++++" + sg_test.tv3.getText());
        }
    }

但是我在屏幕上看到的是空白屏幕..而不是应该显示的自定义控件。 请告诉我我要去哪里错了。

提前致谢。

首先,您不能将子Views添加到View的子类中,因为它没有addView方法。 相反,您应该扩展ViewGroup或其子类之一(如LinearLayoutRelativeLayout等)。

完成上述操作后,您可以使用以下命令简单地添加视图:

View v = inflater.inflate(R.layout.threeradiobutton, this, true);

实际将膨胀后的布局添加到自定义View

现在,由于看不到任何内容,因此您在屏幕上看不到任何内容,您的自定义View为空。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM