【问题标题】:Listview inside TabHost (ActivityGroup)TabHost(ActivityGroup)内的Listview
【发布时间】:2014-05-30 20:28:09
【问题描述】:

我已经创建了 listview 活动。代码如下:

public class List extends Activity {

  ListView listView ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view_android_example);
        listView = (ListView) findViewById(R.id.list);
        String[] values = new String[] { "Android",
                "iPhone",
                "WindowsMobile",
                "Blackberry",
                "WebOS",
                "Ubuntu",
                "Windows7",
                "Max OS X"
        };

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, values);

        listView.setAdapter(adapter);
    }

}

activity_list_view_android_example.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" >

    <ListView
            android:id="@+id/list"
            android:layout_height="wrap_content"
            android:layout_width="match_parent">
    </ListView>

</LinearLayout>

我尝试将此列表活动添加到 tabhost 活动中。 代码如下:

public class Main extends ActivityGroup{

TabHost tabHost;

 public void onCreate(Bundle savedInstanceState) {

        try
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            tabHost=(TabHost)findViewById(R.id.tabhost1);
            tabHost.setup(getLocalActivityManager());
            this.tabHost.setOnTabChangedListener(this);

            TabHost.TabSpec photospec = tabHost.newTabSpec("Tab1");
            photospec.setIndicator("Tab1");
            Intent photosIntent = new Intent(this,Lista.class );
            photospec.setContent(photosIntent);

            TabHost.TabSpec photospec2 = tabHost.newTabSpec("Tab2");
            photospec2.setIndicator("Tab2");
            Intent photosIntent2 = new Intent(this,Lista.class );
            photospec2.setContent(photosIntent2);

            TabHost.TabSpec photospec3 = tabHost.newTabSpec("Tab3");
            photospec3.setIndicator("Tab3");
            Intent photosIntent3 = new Intent(this,Lista.class );
            photospec3.setContent(photosIntent3);


            tabHost.addTab(photospec);
            tabHost.addTab(photospec2);
            tabHost.addTab(photospec3);

        }
        catch (Exception ex)
        {
            String a= ex.getMessage();
            String b="sdds";
        }

    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>

<ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <TabHost
                android:id="@+id/tabhost1"
                android:background="@null"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
            <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">
                <TabWidget
                        android:id="@android:id/tabs"
                        android:layout_width="fill_parent"
                        android:layout_height="40dp"
                        />
                <FrameLayout
                        android:id="@android:id/tabcontent"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        >
                </FrameLayout>
            </LinearLayout>
        </TabHost>
</ScrollView>

所以当我运行我的应用程序时,有东西正在剪切我的列表视图,因此我的设备会显示这个。 https://www.dropbox.com/s/iqbm37lfedklafg/zakladki_lista_zle.png.jpg

作为结果,我希望收到以下申请: https://www.dropbox.com/s/qdjqgnwqjtjvf3d/zakladki_lista_dobre.png.jpg

如何解决我的问题??如果有人帮助我解决我的问题,我将不胜感激。对不起,如果这个问题很简单,但我是初学者,我无法解决这个问题。

【问题讨论】:

  • 我对您的代码感到困惑。不应该是:this,List.class 不是this,Lista.class?这里没有定义Lista 类....

标签: android listview android-tabhost


【解决方案1】:

ListView 设置android:layout_height="match_parent"(而不是wrap_content)。您希望它填充其父级的整个垂直空间。 ListViews 不能使用wrap_content 作为高度。

您还应该删除根ScrollView。嵌套滚动在 Android 中是不可能的(并且 ListView 具有隐式滚动)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多