我正面临这个问题,我想在第一张照片的特定位置替换CoordinatorLayout中的CardView
但是当我使用layout_gravity= "center"
将CardView居中时,视图将像第二张照片
我需要像第一张照片一样定位卡片视图..我怎么能这样做?
这是我的代码
<?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:background="@color/bgColorPrimaryBlue"
android:orientation="vertical"
tools:context="****">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="250dp">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapse_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradiant_background"
app:collapsedTitleGravity="center"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="exitUntilCollapsed|scroll">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profile_image"
android:layout_width="@dimen/profile_image_height"
android:layout_height="@dimen/profile_image_height"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:src="@drawable/worker"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7" />
<TextView
android:id="@+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:fontFamily="@font/el_messiri"
android:paddingTop="140dp"
android:text="User name"
android:textSize="25sp"
android:textStyle="bold"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7" />
<TextView
android:id="@+id/user_job"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:fontFamily="@font/el_messiri"
android:paddingTop="175dp"
android:text="Desc"
android:textSize="20sp"
android:textStyle="bold"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7" />
<android.support.v7.widget.Toolbar
android:id="@+id/profile_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/Base.ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.CardView
android:layout_width="300dp"
android:layout_height="70dp"
android:layout_gravity="center"
app:cardBackgroundColor="@color/bgColorPrimaryBlue">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_thumb_up_black"
android:text="123"
android:textColor="@color/colorPrimaryBlue"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_thumb_down_black_24px"
android:text="123"
android:textColor="@color/colorPrimaryBlue"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_block_black_24px"
android:text="123"
android:textColor="@color/colorPrimaryBlue"
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</android.support.design.widget.CoordinatorLayout>
提前致谢
答案 0 :(得分:1)
试试这个
<?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:background="@color/colorAccent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="250dp">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapse_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:collapsedTitleGravity="center"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="exitUntilCollapsed|scroll">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profile_image"
android:layout_width="96dp"
android:layout_height="96dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:src="@drawable/abc"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7" />
<TextView
android:id="@+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="140dp"
android:text="User name"
android:textSize="25sp"
android:textStyle="bold"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7" />
<TextView
android:id="@+id/user_job"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="175dp"
android:text="Desc"
android:textSize="20sp"
android:textStyle="bold"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7" />
<android.support.v7.widget.Toolbar
android:id="@+id/profile_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/Base.ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.CardView
android:layout_width="300dp"
android:layout_height="70dp"
android:layout_gravity="center"
android:layout_marginTop="220dp"
app:cardBackgroundColor="@color/colorPrimaryDark"
app:layout_anchor="@id/appbar"
app:layout_anchorGravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_launcher_background"
android:text="123"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_launcher_background"
android:text="123"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/ic_launcher_background"
android:text="123"
android:textColor="@color/colorPrimaryDark"
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</android.support.design.widget.CoordinatorLayout>
<强>输出强>
答案 1 :(得分:0)
我通过在nestedScrollView
中使用上限值的减值来解决问题<android.support.v4.widget.NestedScrollView
android:id="@+id/nested_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="-35dp"
app:layout_anchorGravity="top"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<!--CardView and all of it's childs-->
</android.support.v4.widget.NestedScrollView>
使用此
以编程方式对卡片视图进行居中DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;
final int cardMarginLeft = (width - cardView.getLayoutParams().width) / 2;
ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) cardView.getLayoutParams();
layoutParams.leftMargin = cardMarginLeft;
可能有更好的方法来解决这个问题......但这个方法对我有用。