2016-12-04 104 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中。

相關問題