gpt4 book ai didi

android - 自定义 View 不膨胀

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:35:41 25 4
gpt4 key购买 nike

我正在尝试通过 xml 创建我的自定义 View ,但屏幕不显示我的 View ,因为它没有膨胀。我的自定义 View 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;
}
}
};

}

在我必须显示 View 的基本 xml 布局中,是一个按钮,而我的 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"

/>

谁能告诉我这是怎么回事?

最佳答案

您什么也看不到,因为 MyCustomView 没有您编写的任何内容。首先,我将使用 ViewGroups 子项之一而不是 View(如 LinearLayoutRelativeLayout 等):

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);
}
// ...

关于android - 自定义 View 不膨胀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10493729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com