android - 在布局 xml 中根据 android 中的宽度设置 View 高度

标签 android android-layout android-linearlayout

View 是否有可能使其高度与宽度相同?

或用于此目的的任何其他布局,因为对于矢量图像,必须提供宽度和高度。

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/googleLoginBtn"
            android:orientation="horizontal"
            android:layout_centerVertical="true">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical">


                <ImageView
                    android:layout_width="fill_parent"
                    android:layout_height="<height should be equal to the width>"
                    app:srcCompat="@drawable/simple"
                    tools:ignore="MissingPrefix"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Snappy"
                    android:textColor="#FF4081"
                    android:textSize="30dp"
                    android:singleLine="true"
                    android:gravity="center"
                    android:layout_margin="10dp"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:text="Type less, do more. Fastest way to transfer money &amp; make other transactions."
                    android:layout_margin="10dp"/>

            </LinearLayout>


            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:srcCompat="@drawable/simple"
                    tools:ignore="MissingPrefix"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Smart"
                    android:textColor="#FF4081"
                    android:textSize="30dp"
                    android:gravity="center"
                    android:layout_margin="10dp"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Type less, do more. Fastest way to transfer money &amp; make other transactions."
                    android:layout_margin="10dp"/>

            </LinearLayout>
</LinearLayout>

最佳答案

更新:

我会使用 ConstraintLayout今天使用属性 app:layout_constraintDimensionRatio="1:1"

旧答案:

如果不使用自定义 ImageView,则不可能完全在 xml 中。这将是一个解决方案:

public class SquareImageView extends ImageView {
  @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = getMeasuredWidth();
    setMeasuredDimension(width, width);
  }
}

但是,如果您的图像已经是 Square,您可以使用普通的 ImageView:

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true" />

第三种解决方案是直接在您的代码中设置宽度和高度:

LayoutParams params = imageView.getLayoutParams();
params.height = params.width ;
imageView.setLayoutParams(params);

关于android - 在布局 xml 中根据 android 中的宽度设置 View 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36728553/

相关文章:

javascript - javascript 或 css 中的 Android 屏幕尺寸检测 - 没有得到我所期望的

android - 为什么 center_horizo​​ntally 不水平居中元素?

Android - 包括 xml 中的自定义线性布局

android - 如何将 LinearLayout 转换为图像?

android - 如何在线性布局中垂直分配权重?

安卓 TabLayout : distribute evenly

android - 如何在Android中制作透明(alpha)渐变蒙版以使一层淡入透明?

android - android中的自定义布局

android - 以编程方式设置 android 形状颜色

android - Android 游戏开发的开源