结合 layout_weight 和 maxHeight

Cho*_*tch 5 android

我对 Android 编程还很陌生,我遇到了一个简单的问题。

我有一个带有一些按钮和徽标的基本主页。我使用 aLinearLayoutlayout_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>
Run Code Online (Sandbox Code Playgroud)

(注:@dimen/null_height0dp

我的问题是我希望图像随着屏幕的大小缩放,但我不希望它像素化:我设置的最大高度为200px

以下几行似乎不起作用,因为图像没有限制并且完全忽略了该maxHeight属性。

android:layout_weight="1"
android:maxHeight="@dimen/img_maxHeight"
Run Code Online (Sandbox Code Playgroud)

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

主页

它完美地工作,但是当我设置maxHeight10px例如,没有什么变化。

我怎样才能做到这一点?我非常喜欢使用LinearLayout.

先感谢您。

M S*_*dag 2

如果你想为你的图像设置精确的高度,你应该使用weightSum属性和权重

编辑:

对于固定高度,不给出weightimageview采用父布局,然后imageview为该线性布局(父布局)添加权重,并为imageview给出高度作为换行内容和maxHeight。当您weightheight="0dp"内容一起使用时,将捕获整体height,因此它将忽略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>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述