自定义滚动视图未出现
我有一个自定义 ScrollView,如果它是模拟器中唯一的东西,它可以正常工作。
例如,这工作正常:
public class Timeline2Activity extends Activity {
private TimelineView tlv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tlv = new TimelineView(this);
setContentView(tlv);
}
}
但是如果我首先添加小部件,那么它就不会出现。我已经搜索并尝试了两天,但没有任何效果。
这是自定义 ScrollView 的类声明:
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 中看到调试输出。
我束手无策,希望有人能提供一些线索。这似乎是一个常见的基本需求,当然值得谷歌教程。
I have a custom ScrollView that works fine if it is the only thing in the emulator.
For example, this works fine:
public class Timeline2Activity extends Activity {
private TimelineView tlv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tlv = new TimelineView(this);
setContentView(tlv);
}
}
But if I first add widgets then it fails to appear. I've searched and experimented for 2 days and nothing works.
Here is the class declaration of the custom ScrollView:
public class TimelineView extends ScrollView implements OnTouchListener {
And here are the constructors:
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);
}
Here is the 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>
And here is the main program:
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.
}
}
}
The lines commented out are stuff I've tried but didn't work either.
This code correctly puts 3 buttons and a spinner in a horizontal row across the top. But TimelineView fails to appear below them. I know that TimelineView is running because I can see debugging output in the LogCat that I put in.
I'm at my wits end and hope someone can shed some light. This seems like a common fundamental need and sure deserves a google tutorial.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在使用 LinearLayout 并且对第一个元素使用了
android:layout_height="fill_parent
" ,因此第一个元素将填充整个屏幕高度,并且第二个元素没有空间(com.projects .timeline2.TimelineView)这就是为什么您无法看到第二个元素的原因。改用相对布局。并为第二个元素提及
android:layout_alignParentBottom="true"
,以便它始终保持在屏幕的底部,并通过放置android:layout_above< 将第一个元素与第二个元素的上方对齐/code> 到第一个元素
您的 XML 将是
You are using LinearLayout and you have used
android:layout_height="fill_parent
" to the first element so the first element will fill the whole screen height and there will be no space for the second element (com.projects.timeline2.TimelineView) thats why you are unable to see second element.Use Relative Layout instead. and mention
android:layout_alignParentBottom="true"
for second element so that it will always stay to the buttom of the screen and align the first element to the above of second by puttingandroid:layout_above
to first elementYour XML will be
我认为你应该将TimeLine的布局高度更改为xml中的fill_parent,或者向其中添加一些内容。
I think you should change TimeLine's layout height to fill_parent in xml, or add some content to it.
我终于明白了。这是有效的 main.xml。
感谢大家让我朝着正确的方向前进!
I finally got it. Here is the main.xml that worked.
Thanks everyone for getting me going in the right direction!