按钮上带有LinearLayout的Android ViewPager

时间:2012-08-05 17:01:09

标签: android android-layout android-viewpager android-listfragment

我有一个Activity,它使用ViewPager翻阅两个ListFragments - 这很有效。

我正在尝试在底部添加一些具有EditText,Button和TextView的布局。我希望将其固定在屏幕底部,而不是使用ListFragment滚动。

我的观点如下:

<?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"
    android:background="@color/white">

    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

但是因为我在onCreate中使用了这个

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mViewPager = new ViewPager(this);
    mViewPager.setId(R.id.pager);

    setContentView(mViewPager);
}

甚至没有阅读此视图。问题是我不确定如何使用ViewPager并指定布局,允许我在底部添加这样的布局..任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

您似乎将内容视图设置为mViewPager,但未使用该布局。

使用您已创建的布局xml,而不是创建新的ViewPager对象。如果它被称为activity_main.xml,请通过setContentView(R.layout.activity_main);

进行设置

也许是这样的?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@color/white">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" />

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_below="@+id/pager"
    android:orientation="horizontal"
>
<Button... your button here />
<TextView ... your text here />
</LinearLayout>

</RelativeLayout >

答案 1 :(得分:1)

想出来,只是正常使用setConentView,并通过id引用ViewPager ..不知道为什么我有这个问题..?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.application);

    mViewPager = (ViewPager) findViewById(R.id.pager);

    mTabsAdapter = new OURSVPTabsAdapter(this, mViewPager);

    ...
}
相关问题