2016-12-04 106 views
1

我以编程方式在ViewPager中添加视图但是,当我这样做时,我的布局不会在操作栏下面开始。它与操作栏重叠。在android工具栏中添加视图

这是应用程序的屏幕截图。

enter image description here

在我的传呼机适配器我已经写了这个代码。

ProductPagerAdapter.java

@Override 
    public Object instantiateItem(ViewGroup container, int position) { 

     rate = list.get(position); 

     inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View v = inflater.inflate(R.layout.fragment_product, container,false); 

     tvProdName = (TextView) v.findViewById(R.id.tvProdName); 
     tvProdAmount = (TextView) v.findViewById(R.id.tvProdAmount); 
     tvProdDesc = (TextView) v.findViewById(R.id.tvProdDesc); 
     ivProdImage = (ImageView) v.findViewById(R.id.ivProdImage); 

     tvProdName.setText(rate.Name); 
     tvProdAmount.setText(rate.amount); 
     tvProdDesc.setText(rate.longDescription); 
     Picasso.with(context).load(rate.image).into(ivProdImage); 

     ((ViewPager) container).addView(v); 

     return v; 
    } 

fragment_product.xml

<LinearLayout 
    android:orientation="vertical" 
    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"> 

    <include layout="@layout/content_details"/> 

</LinearLayout> 

活性XML

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.ashishkudale.linksolutions.DetailsActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 

    <!--<include layout="@layout/content_details"/>--> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/productPager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@android:drawable/ic_dialog_email" /> 

</android.support.design.widget.CoordinatorLayout> 

有些人可以告诉我如何以编程方式在动作栏下面开始布局。

+0

您可以添加活动xml吗? – fbwnd

+0

看到我编辑的问题 –

回答

2

当您使用的是CoordinatorLayout时,它将视图置于彼此之上,如FrameLayout,最近添加的孩子在上面。要解决您的问题,您必须将android:layout_marginTop="?attr/actionBarSize"添加到您的ViewPager

<android.support.v4.view.ViewPager 
    android:id="@+id/productPager" 
    android:layout_marginTop="?attr/actionBarSize" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 
+0

android:layout_marginTop =“?attr/actionBarSize” 这节省了我很多时间。 –

0

尝试将app:layout_behavior="@string/appbar_scrolling_view_behavior"添加到您的ViewPageractivity.xml中。

相关问题