如何通过约束布局为图像视图设置完美的宽高比

时间:2017-03-28 06:13:30

标签: android android-layout

我正在尝试制作一张其孩子我选择约束布局的卡片。虽然我需要将图像视图作为所有尺寸设备的1:1宽高比。我能够成功实现它,但面临的问题是imageview从屏幕移出。 请参阅以下链接,以便轻松了解我的问题。

这是我的cardview xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="4dp"
    android:background="@android:color/background_light"
    android:clickable="true"
    android:foreground="?android:attr/selectableItemBackground"
    app:cardCornerRadius="2dp"
    app:cardElevation="2dp"
    app:cardUseCompatPadding="true">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:layout_editor_absoluteX="3dp"
        tools:layout_editor_absoluteY="4dp">

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginEnd="1dp" //this line creating issue to me
            android:scaleType="centerCrop"
            app:layout_constraintDimensionRatio="w,1:1"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:layout_constraintLeft_creator="1"
            tools:layout_constraintRight_creator="1"
            app:layout_constraintHorizontal_bias="0.0" />

        <TextView
            android:id="@+id/textView6"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="8dp"
            android:singleLine="true"
            android:text="TextView"
            android:textSize="18sp"
            android:textStyle="bold"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView4"
            tools:layout_constraintBottom_creator="1"
            tools:layout_constraintLeft_creator="1"
            android:layout_marginLeft="24dp" />

        <TextView
            android:id="@+id/textView7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textSize="14sp"
            app:layout_constraintLeft_toLeftOf="@+id/textView6"
            tools:layout_constraintBottom_creator="1"
            tools:layout_constraintLeft_creator="1"
            android:layout_marginLeft="0dp"
            android:layout_marginTop="8dp"
            app:layout_constraintTop_toBottomOf="@+id/textView6" />

        <TextView
            android:id="@+id/textView8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingBottom="16dp"
            android:text="TextView"
            android:textColor="@color/colorAccent"
            android:textSize="14sp"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintLeft_toLeftOf="@+id/textView7"
            app:layout_constraintRight_toRightOf="@+id/textView7"
            tools:layout_constraintBottom_creator="1"
            tools:layout_constraintLeft_creator="1"
            tools:layout_constraintRight_creator="1"
            android:layout_marginTop="8dp"
            app:layout_constraintTop_toBottomOf="@+id/textView7" />
    </android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>

here whenever i remove/set layout_marginEnd or layout_marginStart to 0dp, image view slightly shifted out of screen

here i need to set layout_marginEnd or layout_marginStart to 1dp for seeing perfect square inside screen but after running this app i am seeing this 1dp margin at right of card....it looks so wierd how to resolve this issue?

1 个答案:

答案 0 :(得分:0)

如果要创建完美的正方形,可以使用以下布局

public class SquareFrameLayout extends FrameLayout {
public SquareParentFrameLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if (widthMeasureSpec > heightMeasureSpec) {
        super.onMeasure(heightMeasureSpec, heightMeasureSpec);
    } else {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }
 }
}