这是新代码,它不会抛出异常,但我看不到我的自定义控件“Second”。这是新代码:
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]),它会变得更糟:editText和activity_main.xml中的Button不可见,我得到一个空白布局。
如何将自定义控件添加到activity_main.xml并使其对用户可见?
答案 0 :(得分:0)
您选择Second
直接从ViewGroup
继承。自从您这样做以来,Second
需要真正的ViewGroup
,真正的onLayout()
实施以及其他所有与ViewGroup
相关的内容}。不要随意使用do-nothing实现来覆盖方法,然后在do-nothing实现什么也不做的时候抱怨。
在Android中创建“自定义控件”是一个相对高级的主题。 Android的新手应该专注于他们认为“自定义控件”将以某种方式解决的任何问题的其他解决方案。