2013-04-25 100 views
1

這是新代碼,它不會引發異常,但我看不到我的自定義控件「Second」。這是新代碼:爲什麼自定義控件在activity_main.xml中不可見

activity_main.xml中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layoutRoot" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:id="@+id/L1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" > 

     <Button 
      android:id="@+id/addButton" 
      android:layout_width="74dp" 
      android:layout_height="wrap_content" 
      android:text="Add" /> 

     <EditText 
      android:id="@+id/newEditText" 
      android:layout_width="174dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="3.24" 
      android:ems="10" 
      android:inputType="text" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/List" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 
    </LinearLayout>  
</LinearLayout> 

這是自定義控制:second.xml:

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/secodView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

      <Button 
       android:id="@+id/expandButton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.44" 
       android:text = "Expand"/> 

      <TextView 
       android:id="@+id/txtView" 
       android:layout_width="253dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.56" 
       android:ems="10" 
       android:text = "LOL"/> 
     </LinearLayout> 

     <RadioGroup 
      android:id="@+id/ratingRadioGroup" 
      android:layout_width="321dp" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

      <RadioButton 
       android:id="@+id/likeButton" 
       android:layout_width="145dp" 
       android:layout_height="wrap_content" 
       android:checked="true" 
       android:text="Like" /> 

      <RadioButton 
       android:id="@+id/dislikeButton" 
       android:layout_width="147dp" 
       android:layout_height="wrap_content" 
       android:text="Dislike" /> 
     </RadioGroup> 
</LinearLayout> 

Second.java類:

public class Second extends ViewGroup 
{ 
    private Button mExpandButton; 
    private RadioButton mLikeButton; 
    private RadioButton mDislikeButton; 
    private TextView mText; 
    private RadioGroup mLikeGroup; 
    private ViewGroup vGroup; 

    OnClickListener myClickListener = new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      String s= mExpandButton.getText().toString(); 

      if (s.equals("One Click")) 
       mExpandButton.setText("Other Click"); 
      else 
       mExpandButton.setText("One Click"); 
     } 
    }; 

    OnCheckedChangeListener myCkeckListener = new OnCheckedChangeListener() 
    { 
     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) 
     { 
      if (mLikeButton.isChecked()) 
       mText.setText("I Like it"); 

      if (mDislikeButton.isChecked()) 
       mText.setText("I Don't Like it"); 
     } 
    }; 

    public Second(Context context) 
    { 
     super(context); 
     LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View view = LayoutInflater.from(context).inflate(R.layout.second, this, true); 

     mText = (TextView)this.findViewById(R.id.txtView); 
     mExpandButton = (Button) this.findViewById(R.id.expandButton); 
     mLikeGroup = (RadioGroup)this.findViewById(R.id.ratingRadioGroup); 
     mExpandButton.setOnClickListener(myClickListener); 
     mLikeGroup.setOnCheckedChangeListener(myCkeckListener); 

     //inflater.inflate(R.layout.second, null); 
    } 

    @Override 
    protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) 
    { 
     // TODO Auto-generated method stub  
    } 
} 

ActivityMain.java類,其中活動開始於:

public class MainActivity extends Activity 
{ 
    protected LinearLayout mList; 
    protected EditText mEditText; 
    protected Button mButton; 
    Second[] sec; 
    boolean unavez; 

    OnClickListener myClickListener = new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     {   
     } 
    }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mList = (LinearLayout)findViewById(R.id.List); 
     mEditText = (EditText)findViewById(R.id.newEditText); 
     mButton = (Button)findViewById(R.id.addButton); 

     sec = new Second[4]; 
     unavez = true; 

     for (int i=0; i<4; i++)  
      sec[i]= new Second(this);  
    } 

    @Override 
    protected void onStart() 
    { 
     super.onStart(); 
    } 

    @Override protected void onResume() 
    { 
     super.onResume(); 

     if (!unavez) 
      return; 

     for (int i=0; i<4; i++) 
     { 
      mList.addView(sec[i]); 
      //setContentView(sec[i]); 
     } 

     unavez = false; 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
} 

運行時,我可以在activity_main.xml中看到Button和EditText,但在second.xml中看不到自定義控件。如果我在onResume()中使用setContentView(sec [i])行而不是mList.addView(sec [i]),它會變得更糟:activity_main.xml中的EditText和Button不可見,並且我得到一個空白布局。

如何將我的自定義控件添加到activity_main.xml並使其對用戶可見?

+0

刪除你的無用操作'onLayout()'方法並重試。 – CommonsWare 2013-04-25 16:24:15

+0

當刪除我的onLayout方法時,我從Eclipse IDE得到這條消息:「類型Second必須實現繼承的抽象方法ViewGroup.onLayout(boolean,int,int,int,int)」。所以我需要OnLayout方法,否則我會得到錯誤。 – 2013-04-25 16:32:21

回答

0

您選擇有Second直接從ViewGroup繼承。既然你這樣做,Second需要是一個真正的ViewGroup,與一個真正的onLayout()實現和其他一切隨着是ViewGroup。不要只是隨機地重寫方法而不執行任何實現,然後在無所事事實現什麼都不做時抱怨。

在Android中創建「自定義控件」是一個相對高級的主題。 Android的新手應該關注其他解決方案,以解決他們認爲「自定義控件」將以某種方式解決的任何問題。

+0

感謝您的信息! – 2013-04-25 19:03:06

相關問題