【发布时间】: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