在 ViewGroup 上设置最大宽度 [英] Setting a maximum width on a ViewGroup
问题描述
如何设置 ViewGroup 的最大宽度?我正在使用 Theme.Dialog
活动,但是,当调整到更大的屏幕时,这看起来不太好,它也有点轻巧,我不希望它占据整个屏幕.>
我尝试了这个建议但无济于事.此外,没有像某些视图那样的 android:maxWidth
属性.
有没有办法限制根 LinearLayout 使其仅为(例如)640 倾角?我愿意为此更改到另一个 ViewGroup.
有什么建议吗?
我所做的一个选择是扩展 LinearLayout 并覆盖 onMeasure 函数.例如:
public class BoundedLinearLayout extends LinearLayout {私人最终 int mBoundedWidth;私人最终 int mBoundedHeight;公共 BoundedLinearLayout(上下文上下文){超级(上下文);mBoundedWidth = 0;mBoundedHeight = 0;}public BoundedLinearLayout(上下文上下文,AttributeSet attrs){超级(上下文,属性);TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BoundedView);mBoundedWidth = a.getDimensionPixelSize(R.styleable.BoundedView_bounded_width, 0);mBoundedHeight = a.getDimensionPixelSize(R.styleable.BoundedView_bounded_height, 0);a.recycle();}@覆盖protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {//根据需要调整宽度int measureWidth = MeasureSpec.getSize(widthMeasureSpec);if(mBoundedWidth > 0 && mBoundedWidth
然后您的 XML 将使用自定义类:
<文本视图android:layout_width="fill_parent"android:layout_height="wrap_content"/></com.youpackage.BoundedLinearLayout>
和 attr.xml 文件入口
<attr name="bounded_width" format="dimension"/><attr name="bounded_height" format="dimension"/></declare-styleable>
这是我现在使用的实际代码.这仍然不完整,但在大多数情况下都有效.
How do I set the maximum width of a ViewGroup? I am using a Theme.Dialog
activity, however, this does not look so good when resized to bigger screens, it's also sort of lightweight and I don't want it taking up the whole screen.
I tried this suggestion to no avail. Also, there is no android:maxWidth
property like some views.
Is there a way to restrict the root LinearLayout so that it is only (for example) 640 dip? I am willing to change to another ViewGroup for this.
Any suggestions?
One option which is what I did is to extend LinearLayout and override the onMeasure function. For example:
public class BoundedLinearLayout extends LinearLayout {
private final int mBoundedWidth;
private final int mBoundedHeight;
public BoundedLinearLayout(Context context) {
super(context);
mBoundedWidth = 0;
mBoundedHeight = 0;
}
public BoundedLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.BoundedView);
mBoundedWidth = a.getDimensionPixelSize(R.styleable.BoundedView_bounded_width, 0);
mBoundedHeight = a.getDimensionPixelSize(R.styleable.BoundedView_bounded_height, 0);
a.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Adjust width as necessary
int measuredWidth = MeasureSpec.getSize(widthMeasureSpec);
if(mBoundedWidth > 0 && mBoundedWidth < measuredWidth) {
int measureMode = MeasureSpec.getMode(widthMeasureSpec);
widthMeasureSpec = MeasureSpec.makeMeasureSpec(mBoundedWidth, measureMode);
}
// Adjust height as necessary
int measuredHeight = MeasureSpec.getSize(heightMeasureSpec);
if(mBoundedHeight > 0 && mBoundedHeight < measuredHeight) {
int measureMode = MeasureSpec.getMode(heightMeasureSpec);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(mBoundedHeight, measureMode);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
Then you XML would use the custom class:
<com.yourpackage.BoundedLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:bounded_width="900dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</com.youpackage.BoundedLinearLayout>
And the attr.xml file entry
<declare-styleable name="BoundedView">
<attr name="bounded_width" format="dimension" />
<attr name="bounded_height" format="dimension" />
</declare-styleable>
EDIT: This is the actual code I am using now. This is still not complete but works in most cases.
这篇关于在 ViewGroup 上设置最大宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!