2012-07-16 2 views
0

iOS의 Segmenttab 컨트롤러처럼 작동하는 사용자 정의 컨트롤이 있습니다.맞춤보기가 활동에 표시되지 않음

은 3 textviews을 갖는 레이아웃은 다음과 같다 :

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: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> 

제어는 다음과 같다 :

<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()); 
     } 
    } 

는하지만 내가 화면에 표시하면 빈 화면이 ..하지 표시해야하는 사용자 정의 컨트롤입니다. 친절하게 내가 어디로 잘못 가고 있는지 말해.

미리 감사드립니다.

답변

1

우선 addView 메서드가 없으므로 Views 하위 개체를 View 하위 클래스에 추가 할 수 없습니다. 대신 ViewGroup 또는 하위 클래스 중 하나 (예 : LinearLayout, RelativeLayout 등)를 확장해야합니다.

위의 당신은 간단한와 뷰를 추가 할 수있는 일을 수행 한 후 :

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

실제로 사용자 정의 View에 팽창 된 레이아웃을 추가 할 수 있습니다.

지금보아야 할 것이 없기 때문에 화면에 무언가가 표시되지 않으며 사용자 정의 View이 비어 있습니다.

+0

이제는 사용자 정의 LinearLayout에 컨트롤을 추가하여 컨트롤을 추가 할 수 있습니다. 내 Activity 레이아웃에 커스텀 LinearLayout을 어떻게 추가 할 수 있습니까? 예. 패키지는 org.xxx.yyy.customcontrol 패키지입니다. –

+0

@ newbie_269 사용자 정의 View가 내부 클래스가 아니면' Luksprog