我对Android编程还比较陌生,现在遇到一个简单的问题。
我有一个基本的主页,里面有一些按钮和一个标志。我使用LinearLayout
和 layout_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_height
是 0dp
)
我的问题是,我希望图片能够随着屏幕大小进行缩放,但又不想出现像素化问题:我设置了最大高度为200px。
以下代码似乎没有起作用,因为图片没有限制并完全忽略了maxHeight
属性。
android:layout_weight="1"
android:maxHeight="@dimen/img_maxHeight"
实际上它看起来像这样(示例图片):
它完美地工作,但是当我将maxHeight
设置为10px
时,没有任何变化。
我该如何实现这个?我非常喜欢使用LinearLayout
。
提前感谢您。
@dimen/button_default_height
)的值是多少? - M S Gadag0dp
。(我读过,需要这个值才能使layout_weight
生效)。我会编辑我的问题。 - ChostakovitchImageView
,您将其高度设置为wrap content
,将其更改为0dp
。同样地,对于最后一个TextView
也是如此。 - M S Gadagmatch_parent
在逻辑上不起作用,而且我最后一个TextView的大小会随着屏幕大小的变化而自动调整,所以我对它没有任何问题。 - Chostakovitch