【问题标题】:How to set gravity to a ViewPager in a LinearLayout如何在 LinearLayout 中将重力设置为 ViewPager
【发布时间】:2017-07-21 01:30:50
【问题描述】:

我想知道是否存在将重力设置为 LinearLayout 内的 ViewPager 的方法,我尝试了类似的方法,但它给了我一个错误。

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

    <fragment
        android:id="@+id/displayFragment"
        android:name="com.fragProjects.DisplayFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        tools:layout="@layout/fragment_display" />


        <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="1"
            android:id="@+id/viewPager">
        </android.support.v4.view.ViewPager>

    <fragment
        android:id="@+id/basicFragment"
        android:name="com.fragProjects.BasicFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.2"
        tools:layout="@layout/fragment_basic" />

</LinearLayout>

我需要 ViewPager 位于两个 Fragment 的中间,并且我需要它们每个都具有相同的大小,这就是我尝试使用重力的原因。

如果需要声明 ViewPager 的 MainActivity 如下:

    import android.net.Uri;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentStatePagerAdapter;
    import android.support.v4.view.PagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;

public class MainActivity extends FragmentActivity implements AdvancedFragment.OnFragmentInteractionListener {

    //Number of pages that advanced function ViewPager has.
    private static final int NUM_PAGES = 2;
    //ViewPager that will allow us to swip to access 'shift' advances options.
    protected ViewPager mViewPager;
    //The adapter of previous ViewPager is this.
    protected PagerAdapter mPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Instantiate ViewPager and PagerAdapter
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setCurrentItem(0);
        mViewPager.getCurrentItem();

    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }


    /**
          * A simple pager adapter that represents 2 ScreenSlidePageFragment objects, in
          * sequence.
          */
    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0:
                    return AdvancedFragment.newInstance("", "Page # 1");
                case 1:
                    //return
                    return AdvancedFragment.newInstance("","");
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return NUM_PAGES;
        }
    }
}

如果我从 ViewPager 中删除 layout_gravity 参数并运行应用程序,ViewPager 会占用所有屏幕,就像我们使用“match_parent”设置它的高度一样,但我们没有设置它...

在 ViewPagers 中正常吗?是否存在解决方案? 问候!

【问题讨论】:

    标签: android android-layout android-fragments android-viewpager


    【解决方案1】:

    这是因为您的fragment 中的android:layout_weight。你应该添加

    android:layout_weight="1"
    android:layout_height="0dp"
    

    致您所有的fragments 和LinearLayout 内的浏览器。

    要创建一个线性布局,其中每个孩子在屏幕上使用相同数量的空间,请将每个视图的 android:layout_height 设置为“0dp”(对于垂直布局)或每个视图的 android:layout_width 设置为“ 0dp”(用于水平布局)。然后将每个视图的 android:layout_weight 设置为“1”。

    您可以在Google Layout Weight Guide了解更多关于体重的信息

    【讨论】:

      【解决方案2】:

      是的,viewpager 将占据整个屏幕。如果你不想让它占据整个屏幕。请看一下对应PagerAdapter中的getPageWidth方法。覆盖它并返回例如0.75f 让所有子页面仅跨越 ViewPager 宽度的 75%。

      【讨论】:

        【解决方案3】:

        试试这个

        <?xml version="1.0" encoding="utf-8"?>
        <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:tools="http://schemas.android.com/tools"
        android:fillViewport="true"
               >
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            tools:context="com.fragProjects.MainActivity">
        
            <fragment
            android:id="@+id/displayFragment"
            android:name="com.fragProjects.DisplayFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            tools:layout="@layout/fragment_display" />
        
        
            <android.support.v4.view.ViewPager
                android:id="@+id/viewPager"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"></android.support.v4.view.ViewPager>
        
            <fragment
            android:id="@+id/basicFragment"
            android:name="com.fragProjects.BasicFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.2"
            tools:layout="@layout/fragment_basic" />
        
        </LinearLayout>
        

        【讨论】:

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