我正在尝试通过xml创建自定义视图,但屏幕不显示我的视图,它不会膨胀。我的自定义视图xml如下:
<?xml version="1.0" encoding="utf-8"?>
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/profileSwitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imgAdvertise"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@android:drawable/btn_minus" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<TextView
android:id="@+id/txtAdvertise"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="asdas" />
</LinearLayout>
</ViewSwitcher>
我的自定义View
课程为:
public class MyCustomView extends View{
TextView textAdvertise;
ImageView imageAdvertise;
View view;
ViewSwitcher switcher ;
public MyCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (layoutInflater != null) {
view = layoutInflater.inflate(R.layout.main, null);
}
initialize();
}
public void initialize() {
// TODO Auto-generated method stub
textAdvertise = (TextView) view.findViewById(R.id.txtAdvertise);
imageAdvertise = (ImageView) view.findViewById(R.id.imgAdvertise);
switcher = (ViewSwitcher) view.findViewById(R.id.profileSwitcher);
startAnimation();
}
public void startAnimation() {
// TODO Auto-generated method stub
new Thread() {
public void run() {
for (;;) {
try {
Thread.sleep(2000);
hRefresh.sendEmptyMessage(5);
} catch (Exception e) {
}
}
}
}.start();
}
Handler hRefresh = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 5:
switcher.showNext();
// To go back to the first view, use switcher.showPrevious()
break;
default:
break;
}
}
};
}
在我的基本xml布局中,我必须显示View
,是一个按钮,我的View
为:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:text="Button"
android:layout_weight="1"/>
<com.example.viewswitcher.MyCustomView
android:id="@+id/MyView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
任何人都可以告诉我它出了什么问题?
答案 0 :(得分:9)
您将看不到任何内容,因为MyCustomView
在您编写内容时没有任何内容。首先,我会使用ViewGroups
个孩子而不是View
(例如LinearLayout
,RelativeLayout
等):
public class MyCustomView extends LinearLayout {
TextView textAdvertise;
ImageView imageAdvertise;
View view;
ViewSwitcher switcher ;
public MyCustomView(Context context, AttributeSet attrs) {
super(context);
}
public MyCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (layoutInflater != null) {
view = layoutInflater.inflate(R.layout.main, this, true);
}
initialize();
}
public MyCustomView(Context context, AttributeSet attrs, int theme) {
super(context, attrs, theme);
}
// ...
答案 1 :(得分:3)
使用此选项来扩充您的布局。 的修改
View headerView = View.inflate(this, R.layout.layout_name, null);
这将返回xml文件的父布局,然后您可以直接使用它,或者您可以使用
找到任何其他视图修改
headerView.findViewById(R.id.view_id);
希望它能运作
答案 2 :(得分:0)
我认为您需要的不是自定义视图。也许你应该继承自View的Fragment类。
查看here了解如何实现自己的片段。