gpt4 book ai didi

Android View 相同的宽度和高度

转载 作者:太空宇宙 更新时间:2023-11-03 11:30:43 25 4
gpt4 key购买 nike

以下是我的 xml 代码。在我的 LinearLayout 中。我有两个 subview ,它们使用 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>

最佳答案

通过使用自定义 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"/>

关于Android View 相同的宽度和高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38068751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com