Android 布局溢出组件
当我的Android 布局溢出组件,android,android-layout,Android,Android Layout,当我的TextViews中的字符串增长并溢出时,我会遇到问题,有时会隐藏另一个TextView的文本或开始消失 我希望它能像这样工作(我知道这看起来很像SwigsFlowLayout,但不是很像,因为它的对齐方式不是固定的,一个组件是左对齐的,另一个是右对齐的): 当文本增长时: TextView0 grows: TextView1 grows: ------------------------------------- ------
TextView
s中的字符串增长并溢出时,我会遇到问题,有时会隐藏另一个TextView
的文本或开始消失
我希望它能像这样工作(我知道这看起来很像SwigsFlowLayout
,但不是很像,因为它的对齐方式不是固定的,一个组件是左对齐的,另一个是右对齐的):
当文本增长时:
TextView0 grows: TextView1 grows:
------------------------------------- -------------------------------------
|[TextView0 TextView0 TextView0] | |[TextView0] |
| [TextView1]| | [TextView1 TextView1 TextView1]|
------------------------------------- -------------------------------------
- 是否可以使用现有布局(可能使用布局的组合)
- 如果我真的需要构建一个新的布局,我想有一个工作的例子
我尝试了
relativelayout
和LinearLayout
但从未成功:
RelativeLayout
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/TextView0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="TextView0" />
<TextView
android:id="@+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="TextView1" />
</RelativeLayout>
此处TextView
s开始干扰,当TextView0
获取全宽时TextView1
消失(其宽度趋于零)这是最好的库之一
它简单、易用而且很棒。这是最好的库之一
它简单、易用且令人敬畏。我尝试为此创建一个布局 它的工作原理与我在原始问题中讨论的一样,但如果您能改进,请随时更新答案
public类KeyValueLayout扩展了视图组{
公钥值布局(上下文){
超级(上下文);
}
public KeyValueLayout(上下文,属性集属性集){
super(上下文、属性集);
}
public KeyValueLayout(上下文、属性集、属性集、,
int-defStyle){
super(上下文、属性集、定义样式);
}
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
int paddRight=getPaddingRight();
int paddingft=getPaddingLeft();
int paddBottom=getPaddingBottom();
int paddWidth=paddRight+paddLeft;
int-sizeWidth=MeasureSpec.getSize(widthmasurespec)-paddWidth;
int-sizeHeight=MeasureSpec.getSize(heightMeasureSpec)-paddWidth;
int modeWidth=MeasureSpec.getMode(widthmasurespec);
int modeHeight=MeasureSpec.getMode(heightMeasureSpec);
int modeW=modeWidth==MeasureSpec
?最大测量值:模式宽度;
int modeH=modeHeight==MeasureSpec
?最大测量值:模式高度;
//应放置下一个视图的位置
int lineYPosition=0;
对于(int i=0;i=getChildCount())
打破
View val=getChildAt(i++);
val.measure(
MeasureSpec.MakeMasureSpec(尺寸宽度,型号),
MeasureSpec.MakeMasureSpec(尺寸、重量、型号)
);
LayoutParams valLp=(LayoutParams)val.getLayoutParams();
int valXPosition=palleft+sizeWidth-val.getMeasuredWidth();
//检查两者是否安装在同一条线上
如果(key.getMeasuredWidth()+val.getMeasuredWidth()我试图为此创建布局
它的工作原理与我在原始问题中讨论的一样,但如果您能改进,请随时更新答案
public类KeyValueLayout扩展了视图组{
公钥值布局(上下文){
超级(上下文);
}
public KeyValueLayout(上下文,属性集属性集){
super(上下文、属性集);
}
public KeyValueLayout(上下文、属性集、属性集、,
int-defStyle){
super(上下文、属性集、定义样式);
}
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
int paddRight=getPaddingRight();
int paddingft=getPaddingLeft();
int paddBottom=getPaddingBottom();
int paddWidth=paddRight+paddLeft;
int-sizeWidth=MeasureSpec.getSize(widthmasurespec)-paddWidth;
int-sizeHeight=MeasureSpec.getSize(heightMeasureSpec)-paddWidth;
int modeWidth=MeasureSpec.getMode(widthmasurespec);
int modeHeight=MeasureSpec.getMode(heightMeasureSpec);
int modeW=modeWidth==MeasureSpec
?最大测量值:模式宽度;
int modeH=modeHeight==MeasureSpec
?最大测量值:模式高度;
//应放置下一个视图的位置
int lineYPosition=0;
对于(int i=0;i<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/TextView0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="TextView0" />
<TextView
android:id="@+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="TextView1" />
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/TextView0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView0" />
<TextView
android:id="@+id/TextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="TextView1" />
</LinearLayout>
public class KeyValueLayout extends ViewGroup {
public KeyValueLayout(Context context) {
super(context);
}
public KeyValueLayout(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
public KeyValueLayout(Context context, AttributeSet attributeSet,
int defStyle) {
super(context, attributeSet, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int paddRight = getPaddingRight();
int paddLeft = getPaddingLeft();
int paddBottom = getPaddingBottom();
int paddWidth = paddRight + paddLeft;
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec) - paddWidth;
int sizeHeight = MeasureSpec.getSize(heightMeasureSpec) - paddWidth;
int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
int modeW = modeWidth == MeasureSpec.EXACTLY
? MeasureSpec.AT_MOST : modeWidth;
int modeH = modeHeight == MeasureSpec.EXACTLY
? MeasureSpec.AT_MOST : modeHeight;
// where the next view should be placed
int lineYPosition = 0;
for (int i = 0; i < getChildCount(); ) {
View key = getChildAt(i++);
key.measure(
MeasureSpec.makeMeasureSpec(sizeWidth, modeW),
MeasureSpec.makeMeasureSpec(sizeHeight, modeH)
);
LayoutParams keyLp = (LayoutParams) key.getLayoutParams();
keyLp.setPosition(paddLeft, lineYPosition);
if (i >= getChildCount())
break;
View val = getChildAt(i++);
val.measure(
MeasureSpec.makeMeasureSpec(sizeWidth, modeW),
MeasureSpec.makeMeasureSpec(sizeHeight, modeH)
);
LayoutParams valLp = (LayoutParams) val.getLayoutParams();
int valXPosition = paddLeft + sizeWidth - val.getMeasuredWidth();
// check if both fit on the same line
if (key.getMeasuredWidth() + val.getMeasuredWidth() <= sizeWidth) {
valLp.setPosition(valXPosition, lineYPosition);
lineYPosition += Math.max(key.getMeasuredHeight(),
val.getMeasuredHeight());
} else {
// not enough room, make some space
lineYPosition += key.getMeasuredHeight();
valLp.setPosition(valXPosition, lineYPosition);
lineYPosition += val.getMeasuredHeight();
}
}
int controlMaxLength = sizeWidth + paddRight;
int controlMaxThickness = lineYPosition + paddBottom;
int resolvedWidth = resolveSize(controlMaxLength, widthMeasureSpec);
int resolvedHeight =resolveSize(controlMaxThickness,heightMeasureSpec);
this.setMeasuredDimension(resolvedWidth, resolvedHeight);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
LayoutParams lp = (LayoutParams) child.getLayoutParams();
child.layout(lp.x, lp.y,
lp.x + child.getMeasuredWidth(),
lp.y + child.getMeasuredHeight());
}
}
@Override
protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
return p instanceof LayoutParams;
}
@Override
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attributeSet) {
return new LayoutParams(getContext(), attributeSet);
}
@Override
protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
return new LayoutParams(p);
}
public static class LayoutParams extends ViewGroup.LayoutParams {
private int x;
private int y;
public LayoutParams(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
public LayoutParams(int width, int height) {
super(width, height);
}
public LayoutParams(ViewGroup.LayoutParams layoutParams) {
super(layoutParams);
}
public void setPosition(int x, int y) {
this.x = x;
this.y = y;
}
}
}
<nu.wen.android.layout.KeyValueLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/example_string_long"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/example_string_short" />
</nu.wen.android.layout.KeyValueLayout>