我正在尝试将视图放在容器视图中,以便容器允许视图具有任何高度(和/或宽度,具体取决于您的规范),但不会传递特定的视图。
无法为任何类型的视图定义“maxHeight”。您只能使用确切的大小,使用一些技巧来获取备用大小(layout_weight)或使scrollView占用尽可能多的空间。
唯一类似的是EditText,您可以将“maxLines”与“setMovementMethod”一起使用,如图here所示。
我试图制作包含scrollView的下一个viewGroup,但它没有按预期工作:
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);
}
}
以下是我尝试使用我所做的事情的示例:
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>
代码几乎可以工作,但容器不允许滚动。
我该怎么做才能解决它?
答案 0 :(得分:0)
没有构建行限制器,但在这个问题中有一些解决方案可能有助于解决您的问题 EditText maxLines not working - user can still input more lines than set