Combining layout_weight and maxHeight

I am new to Android programming and am stuck in a simple problem.

I have a basic homepage with a few buttons and a logo. I use the attribute LinearLayoutand layout_weightto distribute all components.

Here is my code:

<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>

(Note: @dimen/null_height- 0dp)

My problem is that I want the image to scale to the screen size, but I don’t want it to be pixel: I set the maximum height to 200px .

The following lines do not seem to work because the image has no limits and completely ignores the attribute maxHeight.

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

Actually it looks like this (example image):

Homepage

, maxHeight 10px, , .

? LinearLayout.

.

+4
3

, layout_weight maxHeight. ImageView onMeasure :

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

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;
    }
}
}
+1

, weightSum

for a fixed height, do not let weightfor imageviewsimply select the parent layout for imageview, then add the weight for this linear layout (parent layout), and for imageviewspecify the height as the contents of the cover and maxHeight. when u uses weights height="0dp", the content will capture all height, so it ignores it maxHeight.

try it

     <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

0
source

All Articles