无法在 Tabhost 派生活动中滚动选项卡内容

发布于 2024-12-18 02:35:44 字数 2767 浏览 3 评论 0 原文

我创建了在 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>

I have created the Tab Layout example found on the Android Developers site Tab Layout in Android 2.2. While the example works as advertised I can not scroll at all on the text views that I have placed in one of the activities. The TextView objects were created dynamically in the given activity if that matters. I have tried wrapping the different portions of the components in main.xml with ScrollView but I either get a build error or a Force Close on launch. Attached is the code that implements the textViews and it's xml as well as main.xml file. I cannot post images since I am new. Any suggestions are greatly appreciated.

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>

Source that creates my TextViews in the Tab:

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 file for above activity:

<?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九八野马 2024-12-25 02:35:44

您是否尝试过将此 xml 用于您的活动:

activity2.xml:

<?xml version="1.0" encoding="utf-8" ?>
<ScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_weight="1"
             android:layout_width="fill_parent"
         android:layout_height="fill_content">
        <LinearLayout                  
                android:layout_width="fill_parent"
                android:id="@+id/layout1"
                android:layout_height="fill_parent" 
                android:orientation="vertical">
        </LinearLayout>
</ScrollView>

然后尝试在 Activity2.class 中添加组件,例如:

setContentView(R.layout.activity2);
...
LinearLayout layout = (LinearLayout)findViewById(R.id.layout1);
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);
        tv.setId(i);
        layout.addView(tv);        
}
...

Have you tried with this xml for your activity:

activity2.xml:

<?xml version="1.0" encoding="utf-8" ?>
<ScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_weight="1"
             android:layout_width="fill_parent"
         android:layout_height="fill_content">
        <LinearLayout                  
                android:layout_width="fill_parent"
                android:id="@+id/layout1"
                android:layout_height="fill_parent" 
                android:orientation="vertical">
        </LinearLayout>
</ScrollView>

And then try adding component in Activity2.class like:

setContentView(R.layout.activity2);
...
LinearLayout layout = (LinearLayout)findViewById(R.id.layout1);
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);
        tv.setId(i);
        layout.addView(tv);        
}
...
昔梦 2024-12-25 02:35:44

使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文