结合layout_weight和maxHeight属性的使用方法

5

我对Android编程还比较陌生,现在遇到一个简单的问题。

我有一个基本的主页,里面有一些按钮和一个标志。我使用LinearLayoutlayout_weight属性来分配所有组件。

这是我的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="iut.project.findmeapp.MainActivity" >

    <ImageView
        android:id="@+id/activityMainAppLogoIv"
        android:layout_width="match_parent"
        android:layout_height="@dimen/null_height"
        android:contentDescription="@string/description_logo"
        android:layout_weight="1"
        android:maxHeight="@dimen/img_maxHeight"
        android:layout_margin="@dimen/img_margin"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/activityMainMyEventsBtn"
        android:layout_width="match_parent"
        android:layout_height="@dimen/null_height"
        android:layout_marginTop="@dimen/button_margin_vertical"
        android:layout_marginBottom="@dimen/button_margin_vertical"
        android:layout_weight="1"
        android:text="@string/my_events" />

    <Button
        android:id="@+id/activityMainMyContactsBtn"
        android:layout_width="match_parent"
        android:layout_height="@dimen/null_height"
        android:layout_marginTop="@dimen/button_margin_vertical"
        android:layout_marginBottom="@dimen/button_margin_vertical"
        android:layout_weight="1"
        android:text="@string/my_contacts" />

    <Button
        android:id="@+id/activityMainLastNotifBtn"
        android:layout_width="match_parent"
        android:layout_height="@dimen/null_height"
        android:layout_marginTop="@dimen/button_margin_vertical"
        android:layout_marginBottom="@dimen/button_margin_vertical"
        android:layout_weight="1"
        android:text="@string/last_notification" />

    <TextView
        android:id="@+id/activityMainCopyrightTv"
        android:layout_width="match_parent"
        android:layout_height="@dimen/null_height"
        android:gravity="center"
        android:layout_weight="0.5"
        android:layout_margin="@dimen/tw_margin"
        android:text="@string/copyright" />

</LinearLayout>

(注意:@dimen/null_height0dp)

我的问题是,我希望图片能够随着屏幕大小进行缩放,但又不想出现像素化问题:我设置了最大高度为200px

以下代码似乎没有起作用,因为图片没有限制并完全忽略了maxHeight属性。

android:layout_weight="1"
android:maxHeight="@dimen/img_maxHeight"

实际上它看起来像这样(示例图片):

主页

它完美地工作,但是当我将maxHeight设置为10px时,没有任何变化。

我该如何实现这个?我非常喜欢使用LinearLayout

提前感谢您。


这个(@dimen/button_default_height)的值是多少? - M S Gadag
@MSGadag 值为 0dp。(我读过,需要这个值才能使 layout_weight 生效)。我会编辑我的问题。 - Chostakovitch
对于您的 ImageView,您将其高度设置为 wrap content,将其更改为 0dp。同样地,对于最后一个 TextView 也是如此。 - M S Gadag
@MSGadag 我该怎么办?match_parent在逻辑上不起作用,而且我最后一个TextView的大小会随着屏幕大小的变化而自动调整,所以我对它没有任何问题。 - Chostakovitch
将其设置为0dp并发布您想要的图像,然后尝试我的答案。 - M S Gadag
3个回答

4

感谢Evripidis Drakos指出正确的方向。 只需检查是否实际需要设置最大高度即可。

public class MaxHeightImageView extends ImageView {

public static final int MAX_HEIGHT_NOT_SET = -1;
public static final int INDEX_MAX_HEIGHT = 0;
private static final int[] STYLE_ATTRIBUTE_ARRAY = {
        android.R.attr.maxHeight,
};

private int myMaxHeight;

public MaxHeightImageView(Context context) {
    super(context);
    init(context, null);
}

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

public MaxHeightImageView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs);
}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public MaxHeightImageView(Context context, AttributeSet attrs, int defStyleAttr,
        int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init(context, attrs);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int size = MeasureSpec.getSize(heightMeasureSpec);
    if(MAX_HEIGHT_NOT_SET != myMaxHeight && size > myMaxHeight) {
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(myMaxHeight, MeasureSpec.AT_MOST);

    }

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

private void init(Context context, AttributeSet attrs) {
    if(context != null) {
        TypedArray ta = context.obtainStyledAttributes(attrs,
                STYLE_ATTRIBUTE_ARRAY);
        myMaxHeight = ta.getDimensionPixelSize(INDEX_MAX_HEIGHT, 0);
    } else {
        myMaxHeight = MAX_HEIGHT_NOT_SET;
    }
}
}

3

我认为你不能将layout_weightmaxHeight组合使用。 你可以尝试子类化ImageView,并重写onMeasure方法,如下所示:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(200, MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

2
如果您想为图像设置精确的高度,则应使用weightSum属性和weight。
编辑:
对于固定高度,请不要为imageview指定weight,只需将父布局用作imageview,然后为该线性布局(父布局)添加weight,并为imageview提供wrap content和maxHeight的高度。当您将weight与height="0dp"一起使用时,内容将占用整个高度,因此它将忽略maxHeight。
请尝试此操作
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:weightSum="5"
    tools:context="iut.project.findmeapp.MainActivity">

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

        <ImageView
            android:id="@+id/activityMainAppLogoIv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:contentDescription="@string/quantity_hint"
            android:maxHeight="200px"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <Button
        android:id="@+id/activityMainMyEventsBtn"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="my_events" />

    <Button
        android:id="@+id/activityMainMyContactsBtn"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="my_contacts" />

    <Button
        android:id="@+id/activityMainLastNotifBtn"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="last_notification" />

    <TextView
        android:id="@+id/activityMainCopyrightTv"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".5"
        android:gravity="center"
        android:text="copyright" />

</LinearLayout>

enter image description here


这是我的错误,我在我的问题中犯了一个错误。实际上,我想要图像在所有屏幕上按比例缩放,直到最大高度为200 px,而不是200 dp(我的问题毫无意义)。所以你的代码是可以工作的,但它不会在图像变得像素化之前停止它(请参见此处的示例,其中有一个巨大的屏幕:http://puu.sh/hgWZT/fb6a1d2f19.png)。我编辑了我的问题。 - Chostakovitch
抱歉,我不理解你的问题。 - Chostakovitch
使用我第一篇帖子中的确切代码和 img_maxHeight10px,它会给出这样的结果:http://puu.sh/hgXAa/4fbf9acfad.png。因此,maxHeight 被完全忽略了,这正是我不想发生的事情。 - Chostakovitch

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