【问题标题】:Android ViewPager with LinearLayout at the button按钮处带有 LinearLayout 的 Android ViewPager
【发布时间】:2012-08-05 17:01:09
【问题描述】:

我有一个 Activity,它使用 ViewPager 在两个 ListFragment 中进行分页——这很好用。

我正在尝试在底部添加某种类型的布局,其中包含 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 并指定布局,允许我在底部添加这样的布局.. 谁能帮帮我?

【问题讨论】:

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


    【解决方案1】:

    看起来您将内容视图设置为mViewPager,它没有使用布局。

    与其创建新的 ViewPager 对象,不如使用您创建的布局 xml。如果它被称为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 >
    

    【讨论】:

    • 也许发布您用于将适配器附加到寻呼机的活动代码?还有什么不工作?
    • 你的建议是对的,但是没有设置 viewpager,viewpager 没有工作,我不得不在 tabsadapter 中设置它。你的答案是一半
    • @scottyab 我正在尝试相同的布局,但现在 viewpager 不起作用,那么我应该在哪里连接适配器?
    【解决方案2】:

    想通了,正常使用 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);
    
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 2018-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-26
      相关资源
      最近更新 更多