【问题标题】:How do I change the cardview position to the bottom of the tablayout?如何将卡片视图位置更改为选项卡布局的底部?
【发布时间】:2017-11-05 15:52:08
【问题描述】:

我试图创建一个由片段中的五个项目组成的卡片视图。但是,cardview 位置始终位于布局的顶部。我尝试使用边距和对齐,但没有用。 如何更改卡片视图的位置?

这是我的 card_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android.support.v7.cardview="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="85dp"
android:orientation="vertical"
android:id="@+id/my_relative_layout"
>


<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:layout_marginRight="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginBottom="8dp"
    android:backgroundTint="#E3F0F5"
    android:layout_centerVertical="true"
    card_view:cardCornerRadius="5dp"
    card_view:elevation="14dp"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/iv_image"
            android:src="@drawable/avatar1"
            android:layout_centerVertical="true"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="10dp"
            />

        <TextView
            android:textColor="#95989A"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_text"
            android:layout_toRightOf="@+id/iv_image"
            android:gravity="center"/>

        <TextView
            android:textColor="#95989A"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_blah"
            android:text="Another RV Fragment"
            android:layout_below="@+id/tv_text"
            android:layout_toRightOf="@+id/iv_image"
            android:layout_toEndOf="@+id/iv_image"/>

    </RelativeLayout>

</android.support.v7.widget.CardView>

</RelativeLayout>

这是我的 activity_pendaftar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
tools:context="com.martin.app.donorkuyadmin.PendaftarActivity"
android:background="@drawable/background4">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <include
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        layout="@layout/toolbar_pendaftar">
    </include>

    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tablayout"
        app:tabMode="fixed"
        app:tabGravity="fill">
    </android.support.design.widget.TabLayout>

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

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

</RelativeLayout>

这是我的截图:

【问题讨论】:

  • 请添加主布局xml文件。
  • @vikashkumarpandey 好的

标签: android android-layout android-cardview


【解决方案1】:

给你的AppBarLayout 一个id(比如,id=action_bar),然后简单地将layout_below="@id/action_bar" 添加到你的ViewPager

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

【讨论】:

    【解决方案2】:

    使用协调器布局而不是相对布局,并将 layout_behavior 属性设置为视图分页器到 appbar_scrolling_view_behavior。

    <?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <include
            layout="@layout/toolbar_pendaftar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <android.support.design.widget.TabLayout
            android:id="@+id/tablayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="fill"
            app:tabMode="fixed"></android.support.design.widget.TabLayout>
    
    </android.support.design.widget.AppBarLayout>
    
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    
    </android.support.design.widget.CoordinatorLayout>
    

    【讨论】:

      【解决方案3】:

      将您的 include 语句移到 appbarlayout 之外,并将其与父级底部对齐,如下所示:

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout
          xmlns:app="http://schemas.android.com/apk/res-auto"
          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"
          tools:context="com.martin.app.donorkuyadmin.PendaftarActivity"
          android:background="@drawable/background4">
      
          <android.support.design.widget.AppBarLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
      
      
              <android.support.design.widget.TabLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:id="@+id/tablayout"
                  app:tabMode="fixed"
                  app:tabGravity="fill">
              </android.support.design.widget.TabLayout>
      
          </android.support.design.widget.AppBarLayout>
      
          <android.support.v4.view.ViewPager
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:id="@+id/viewpager">
          </android.support.v4.view.ViewPager>
      
          <include
              android:layout_height="wrap_content"
              android:layout_width="match_parent"
              layout="@layout/toolbar_pendaftar"
              android:layout_alignParentBottom="true">
          </include>
      
      </RelativeLayout>
      

      【讨论】:

      • 它没有用。它只是改变了我的工具栏的位置,而不是卡片视图:D
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多