如何设置任何形式的视图的最大高度? [英] How to set a max height of a any kind of view?
问题描述
背景
我试图把一个容器视图中的图,从而使容器将允许以具有任何高度(和/或宽度,取决于你规格),但不通过特定的一个。对>
问题
有没有办法界定了maxHeight对任何一种观点。您只能使用确切大小,可以使用一些技巧来作为备用大小(layout_weight)或作出其需要滚动视图采取尽可能多的空间。
唯一类似的事情是EditText上,您可以使用MAXLINES连同setMovementMethod,如图所示 这里 。
我已经试过
我试图使包含滚动视图的下一个ViewGroup中,但预期它没有工作:
公共类SizeLimitLayout扩展的FrameLayout {
私人INT mMaxHeightInPixels = -1;
私人INT mMaxWidthInPixels = -1; 公共SizeLimitLayout(最终上下文的背景下){
超级(上下文);
} 公共SizeLimitLayout(最终上下文的背景下,最终的AttributeSet ATTRS){
超(背景下,ATTRS);
} 公共SizeLimitLayout(最终上下文的背景下,最终的AttributeSet ATTRS,最终诠释defStyle){
超(背景下,ATTRS,defStyle);
} 公共无效setMaxHeight(最终诠释maxHeightInPixels){
this.mMaxHeightInPixels = maxHeightInPixels;
} 公共无效setMaxWidth(最终诠释maxWidthInPixels){
this.mMaxWidthInPixels = maxWidthInPixels;
} @覆盖
保护无效onMeasure(最终诠释widthMeasureSpec,最终诠释heightMeasureSpec){
//时间来决定什么是这一观点的大小
super.onMeasure(widthMeasureSpec,heightMeasureSpec);
INT宽度= getMeasuredWidth(),身高= getMeasuredHeight();
如果(mMaxWidthInPixels> = 0)
宽度= Math.min(mMaxWidthInPixels,宽度);
如果(mMaxHeightInPixels> = 0)
高度= Math.min(mMaxHeightInPixels,高度);
setMeasuredDimension(宽度,高度);
}
}
下面就是我如何试图用什么我做了一个例子:
MainActivity.java
公共类MainActivity扩展ActionBarActivity { @覆盖
保护无效的onCreate(最终捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
最后SizeLimitLayout sizeLimitLayout =(SizeLimitLayout)findViewById(R.id.sizeLimitLayout);
sizeLimitLayout.setMaxHeight((INT)convertDpToPixels(这一点,160));
最后查看BTN = findViewById(R.id.button);
最终的ViewGroup容器=(ViewGroup中)findViewById(R.id.container);
btn.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(最终查看V){
最终的ImageView IV =新ImageView的(MainActivity.this);
iv.setImageResource(R.drawable.ic_launcher);
container.addView(四);
}
});
} 公共静态浮动convertDpToPixels(最终上下文的背景下,最终浮动DP){
最终浮动像素= TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,DP,context.getResources()
.getDisplayMetrics());
返回像素;
}
}
activity_main.xml中
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
工具:上下文=com.example.viewsizelimittest.MainActivity
工具:忽略=MergeRootFrame> <按钮
机器人:ID =@ + ID /按钮
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=点击
工具:忽略=硬codedText/> < com.example.viewsizelimittest.SizeLimitLayout
机器人:ID =@ + ID / sizeLimitLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT> <滚动型
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:背景=#33ff0000
机器人:fillViewport =真正的> <的LinearLayout
机器人:ID =@ + ID /容器
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直>
< / LinearLayout中>
< /滚动型>
< /com.example.viewsizelimittest.SizeLimitLayout>< / LinearLayout中>
问题
在code差不多的作品,但容器不允许滚动。
我能做些什么来解决这个问题?
有没有建立它限行,但这个问题也有一些解决方案,可以帮助您解决问题
<一href=\"http://stackoverflow.com/questions/7092961/edittext-maxlines-not-working-user-can-still-input-more-lines-than-set\">EditText MAXLINES不工作 - 用户仍然可以输入更多的线路不是设置
Background
I'm trying to put a view within a container view, so that the container will allow the view to have any height (and/or width,depending on your specification) , yet not pass a specific one.
The problem
There is no way to define "maxHeight" for any kind of view. You can only use exact size , use some tricks to take the spare size (layout_weight) or make the scrollView take as much space as it needs.
The only similar thing is for EditText , which you can use "maxLines" together with "setMovementMethod", as shown here .
What I've tried
I've tried to make the next viewGroup that contains the scrollView, but it didn't work as expected:
public class SizeLimitLayout extends FrameLayout {
private int mMaxHeightInPixels = -1;
private int mMaxWidthInPixels = -1;
public SizeLimitLayout(final Context context) {
super(context);
}
public SizeLimitLayout(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
public SizeLimitLayout(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
}
public void setMaxHeight(final int maxHeightInPixels) {
this.mMaxHeightInPixels = maxHeightInPixels;
}
public void setMaxWidth(final int maxWidthInPixels) {
this.mMaxWidthInPixels = maxWidthInPixels;
}
@Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
// time to decide what is the size of this view
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMeasuredWidth(), height = getMeasuredHeight();
if (mMaxWidthInPixels >= 0)
width = Math.min(mMaxWidthInPixels, width);
if (mMaxHeightInPixels >= 0)
height = Math.min(mMaxHeightInPixels, height);
setMeasuredDimension(width, height);
}
}
Here's a sample of how I tried to use what I made:
MainActivity.java
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SizeLimitLayout sizeLimitLayout = (SizeLimitLayout) findViewById(R.id.sizeLimitLayout);
sizeLimitLayout.setMaxHeight((int) convertDpToPixels(this, 160));
final View btn = findViewById(R.id.button);
final ViewGroup container = (ViewGroup) findViewById(R.id.container);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
final ImageView iv = new ImageView(MainActivity.this);
iv.setImageResource(R.drawable.ic_launcher);
container.addView(iv);
}
});
}
public static float convertDpToPixels(final Context context, final float dp) {
final float pixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources()
.getDisplayMetrics());
return pixels;
}
}
activity_main.xml
<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"
tools:context="com.example.viewsizelimittest.MainActivity"
tools:ignore="MergeRootFrame" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click"
tools:ignore="HardcodedText" />
<com.example.viewsizelimittest.SizeLimitLayout
android:id="@+id/sizeLimitLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#33ff0000"
android:fillViewport="true" >
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</com.example.viewsizelimittest.SizeLimitLayout>
</LinearLayout>
The question
The code almost works, but the container doesn't allow to scroll .
What can I do to fix it?
There isn't build it row limiter but in this question there are some solutions that might help to solve your problem EditText maxLines not working - user can still input more lines than set
这篇关于如何设置任何形式的视图的最大高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!