我有一个自定义的ScrollView,如果它是模拟器中唯一的东西,它可以正常工作。自定义滚动视图不出现
例如,这工作得很好:
public class Timeline2Activity extends Activity {
private TimelineView tlv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tlv = new TimelineView(this);
setContentView(tlv);
}
}
但是,如果我先添加小部件则没有出现。我已经搜索了2天,并且没有任何效果。
这是自定义滚动型的类声明:
public class TimelineView extends ScrollView implements OnTouchListener {
而且这里的构造函数:
public TimelineView(Context context) {
super(context);
this.setOnTouchListener(this);
setVisibility(VISIBLE);
}
//the following constructor is needed to inflate the view from XML
public TimelineView(Context context, AttributeSet ats) {
super(context,ats);
this.setOnTouchListener(this);
setVisibility(VISIBLE);
}
下面是main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/zoomall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Zoom All" />
<Button
android:id="@+id/zoomout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
<Button
android:id="@+id/zoomin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />
<Spinner
android:id="@+id/category_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/category_prompt"
/>
</LinearLayout>
<com.projects.timeline2.TimelineView
android:id="@+id/timeline_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
这里是主程序:
public class Timeline2Activity extends Activity {
private TimelineView tlv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//tlv = new TimelineView(this);
//setContentView(tlv);
setContentView(R.layout.main);
Spinner spinnerCategory = (Spinner) findViewById(R.id.category_spinner);
ArrayAdapter<CharSequence> adapterCategory = ArrayAdapter.createFromResource(
this, R.array.categories, android.R.layout.simple_spinner_item);
adapterCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCategory.setAdapter(adapterCategory);
spinnerCategory.setOnItemSelectedListener(new MyOnItemSelectedListener());
//TimelineView tlv = (TimelineView) findViewById(R.id.timeline_view);
//ViewGroup container = (ViewGroup) findViewById(R.id.container);
//container.addView(tlv);
}
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext(), "The selection is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
}
注释掉的行是我尝试过的东西,但也没有工作。
此代码正确地将3个按钮和一个微调器放在顶部的水平行中。但TimelineView无法显示在它们下面。我知道TimelineView正在运行,因为我可以在我输入的LogCat中看到调试输出。
我在我的智慧结尾,并希望有人能说出点亮。这似乎是一个普遍的基本需求,当然值得一个谷歌教程。
您能否提供更多关于发生的事情的信息? – JoxTraex 2011-12-16 04:28:23