Android 结合布局重量和最大高度
我对Android编程很陌生,我被一个简单的问题困住了 我有一个基本的主页与一些按钮和一个标志。我使用Android 结合布局重量和最大高度,android,Android,我对Android编程很陌生,我被一个简单的问题困住了 我有一个基本的主页与一些按钮和一个标志。我使用LinearLayout和layout\u weight属性来分布所有组件 这是我的密码: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="
LinearLayout
和layout\u 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>
实际上它看起来像这样(示例图像):
它工作得很好,但是当我将maxHeight
设置为10px
时,没有任何变化
我怎样才能做到这一点?我非常喜欢使用线性布局
提前谢谢。如果你想为你的图像设置准确的高度,你应该使用weightSum属性和weight 编辑: 对于固定高度,不要为
imageview
提供weight
,只需为imageview
获取父布局,然后为该线性布局(父布局)添加权重,对于imageview
提供高度作为包裹内容,并为maxHeight
。当您将weight
与height=“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>
我认为你不能将
布局重量
与最大高度
结合起来。
您可以尝试子类化ImageView
来覆盖onMeasure
方法,如下所示:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(200, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
感谢埃夫里皮迪斯·德拉科斯为我指明了正确的方向。只需要一个检查,只设置最大高度,如果它的实际需要
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;
}
}
}
这个(
@dimen/button\u default\u height
)的值是多少?@MSGadag该值是0dp
。(我已经读到我需要这个值来使layou-weight
工作)。我将编辑我的问题。对于urimageview
ur,给出高度为wrap content
将其设置为0dp
。对于最后一个文本视图too@MSGadag我能做什么match_parent
在逻辑上不起作用,而且我对上一个文本视图没有任何问题,因为它会随着屏幕的大小进行缩放。设置为0dp并发布您所需的图像n尝试我的回答这是我的错,我在问题中犯了一个错误。事实上,我希望图像在所有屏幕上缩放,直到最大高度为200像素,而不是200 dp(我的问题毫无意义)。因此,您的代码可以工作,但不会在图像像素化之前停止图像。(请参见此处带有大屏幕的示例:)。我编辑了我的问题。很抱歉,我不理解你的问题。在我的第一篇文章中有确切的代码和10px的img\u maxHeight
,它给出了:。所以maxHeight
被完全忽略了,这是我不想发生的。
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;
}
}
}