【问题标题】:Add View Below Toolbar in android在android中的工具栏下方添加视图
【发布时间】:2016-12-04 17:40:00
【问题描述】:

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

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

在我的寻呼机适配器中,我编写了这段代码。

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>

能否告诉我如何以编程方式在操作栏下方开始布局。

【问题讨论】:

  • 你能添加你的活动 xml 吗?
  • 查看我编辑的问题

标签: android android-layout android-viewpager android-toolbar


【解决方案1】:

当您使用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" />

【讨论】:

  • android:layout_marginTop="?attr/actionBarSize" 这节省了我很多时间。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2016-01-02
    • 1970-01-01
    • 2018-04-19
    • 2011-02-15
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    相关资源
    最近更新 更多