簡體   English   中英

如何在LinearLayout中將重力設置為ViewPager

[英]How to set gravity to a ViewPager in a LinearLayout

我想知道是否存在一種在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,並且每個片段都具有相同的大小,這就是為什么我嘗試使用重力的原因。

如果您需要在其中聲明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中正常嗎? 是否存在解決方案? 問候!

是的,viewpager會占據整個屏幕。 如果您不希望它占據整個屏幕。 請查看相應的PagerAdapter中的getPageWidth方法。 覆蓋它並返回例如0.75f,以使所有子頁面僅跨越ViewPager寬度的75%。

這是由於您的fragmentandroid:layout_weight 您應該添加

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

LinearLayout所有fragment和viewpager。

要創建每個孩子在屏幕上使用相同空間的線性布局,請將每個視圖的android:layout_height設置為“ 0dp”(對於垂直布局)或將每個視圖的android:layout_width設置為“ 0dp”(用於水平布局)。 然后將每個視圖的android:layout_weight設置為“ 1”。

您可以在此Google版式重量指南中了解有關重量的更多信息

嘗試這個

<?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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM