Android视图相同宽度和高度

4
以下是我的xml代码。 在我的LinearLayout中,我有两个子视图,它们使用weightSum对齐。 我有一个ImageView,由于weightSum的正确对齐,其宽度被对齐,但问题在于高度。 我希望它的高度与其宽度具有相同的尺寸。 有没有不使用静态dp值的解决方案。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp">

    <LinearLayout
        android:background="@drawable/background_gradien_yellow"
        android:weightSum="10"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

            <ImageView
                android:layout_weight="6"
                android:id="@+id/startbtn"
                android:textStyle="bold"
                android:alpha="0.8"
                android:textColor="#fff"
                android:text="@string/start"
                android:layout_centerInParent="true"
               android:background="@drawable/background_gradient_circular"
                android:layout_width="match_parent"
                android:layout_height="150dp"/>


        <LinearLayout
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp"
            android:orientation="vertical"
            android:layout_weight="3"
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <TextView
                android:textColor="@color/White"
                android:id="@+id/tv_challange_title"
                android:textSize="20sp"
                android:text="Early-Bird Challange"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <TextView
                android:id="@+id/tv_challange_desc"
                android:alpha="0.5"
                android:textColor="@color/White"
                android:textSize="15sp"
                android:layout_marginTop="5dp"
                android:text="Take 100 steps five times within.. "
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>


        </LinearLayout>

    </LinearLayout>

</RelativeLayout>
6个回答

10

通过使用自定义 ImageView 找到了解决方案。这是最佳解决方案。在自定义 ImageView 类中将高度设置为与宽度相同。

Java 代码:

public class SquareImageView extends ImageView {

    public SquareImageView(Context context) {
        super(context);
    }

    public SquareImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = getMeasuredWidth();
        setMeasuredDimension(width, width);
    }

}

XML 代码

         <com.mypakage.utils.SquareImageView
            android:background="@drawable/fitbit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

如果这是一个包含子项的布局呢?这段代码将会将它们从父布局中移除。 - Maveňツ

8

我认为你可以使用Activity的Java代码来实现。

像这样:

android.view.ViewGroup.LayoutParams mParams = imageView.getLayoutParams();
mParams.height = imageView.getWidth();
imageView.setLayoutParams(mParams);

2
这也是我想到的,我认为在XML代码中无法实现它,因为权重只能包括一个方向。如果你正确地执行此代码,它将起作用。如果你得到了零宽度,那么只需在post runnable方法或getViewTreeObserver中添加这个即可。 - Rashid

5

制作自定义ImageView,然后将其height设置为与width相同。 查看此答案


它正在拉伸我的父视图。使用自定义ImageView。 - abdul khan
将imageView的scaleType设置为“center”。 - J.Doe

1
你将 ImageView 的 scaleType 设置为 centerInCrop。 android:scaleType="centerInCrop"

他只想设置ImageView的高度和宽度。 - PriyankaChauhan
但是这个答案与调整ImageView的高度和宽度无关。 - Rashid
scaletype属性不会改变大小,线程启动器想要将imageview的大小设置为正方形,这不会调整imageview的大小,而是调整imageview中内容的大小以适应imageview。你明白吗? - Rashid
1
@SecretCoder,如果您需要更多的评论,请前往聊天室。如果您需要像线程一样的更多评论,请前往聊天室。SO不是用于聊天的。 - Smit.Satodia
1
@Smit.Satodia,我们不是在聊天,我们正在讨论。如需更多关注,请前往帮助页面。 - Rashid
显示剩余2条评论

0

你可以尝试这个方法,只需给内部线性布局设置权重即可。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:paddingBottom="5dp"`enter code here`
    android:paddingLeft="5dp"
    android:paddingRight="5dp">

    <LinearLayout
        android:background="@color/colorPrimary"
        android:weightSum="10"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_weight="6"
            android:id="@+id/startbtn"
            android:textStyle="bold"
            android:alpha="0.8"
            android:textColor="#fff"
            android:text="start"
            android:layout_centerInParent="true"
            android:background="@drawable/common_ic_googleplayservices"
            android:layout_width="match_parent"
            android:layout_height="150dp"/>


        <LinearLayout
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp"
            android:orientation="vertical"
            android:layout_weight="3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:weightSum="2">


            <TextView
                android:textColor="#fff"
                android:id="@+id/tv_challange_title"
                android:textSize="20sp"
                android:text="Early-Bird Challange"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <TextView
                android:id="@+id/tv_challange_desc"
                android:alpha="0.5"
                android:textColor="#000"
                android:textSize="15sp"
                android:layout_marginTop="5dp"
                android:text="Take 100 steps five times within.. "
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>


        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

0

使用宽度和高度相同的图像(最好是.9patch)

      <ImageView
        android:layout_weight="6"
        android:id="@+id/startbtn"
        android:textStyle="bold"
        android:alpha="0.8"
        android:textColor="#fff"
        android:text="start"
        android:layout_centerInParent="true"
        android:layout_width="match_parent"

        android:src="@drawable/background_gradient_circular"
        android:scaleType="fitCenter"
        android:layout_height="match_parent" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接