无法在 Tabhost 派生活动中滚动选项卡内容
我创建了在 Android 开发人员网站上找到的选项卡布局示例 选项卡布局 在 Android 2.2 中。虽然该示例按照广告的方式工作,但我根本无法滚动放置在其中一项活动中的文本视图。如果重要的话,TextView 对象是在给定活动中动态创建的。我尝试使用 ScrollView 将组件的不同部分包装在 main.xml 中,但我要么收到构建错误,要么在启动时强制关闭。附件是实现textViews 的代码及其xml 以及main.xml 文件。由于我是新人,所以无法发布图片。非常感谢任何建议。
Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<!-- android:padding="5dp" -->
</LinearLayout>
</TabHost>
在 Tab: xml 文件中为上述活动创建 TextView 的源
public class Activity2 extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
DisplayTexts(20);
}
private void DisplayTexts(int length){
if(length == 0)
return;
LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
for(int i = 0; i < length; i++)
{
TextView tv = new TextView(getApplicationContext());
registerForContextMenu(tv);
tv.setTextSize(30);
tv.setText("TextView " + String.valueOf(i));
tv.setLayoutParams(params);
layout.addView(tv);
tv.setId(i);
}
LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
this.addContentView(layout, layoutParam);
}
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试过将此 xml 用于您的活动:
activity2.xml:
然后尝试在 Activity2.class 中添加组件,例如:
Have you tried with this xml for your activity:
activity2.xml:
And then try adding component in Activity2.class like:
使用 SetContentView 代替 addContentView 并尝试为布局设置 canScrollVertically (int 方向)。
listView 可能是您想要完成的任务的替代方案。
Instead of addContentView, use SetContentView and try setting canScrollVertically (int direction) for the layout.
A listView could be an alternative for what you are trying to accomplish.