线性布局中有两个文本视图,水平方向。
LeftText | RightText
目标是充分利用屏幕空间并尽可能多地显示文本值。如果文本超出文本视图大小,则应使用省略号截断它们。如果其中一个是短的,另一个应该扩展到剩余的大小。
例如
输入 - 短|短
预期输出短|短
输入 - 短| LongLongLongLong
预期输出短|龙...
输入 - LongLongLongLong |短
预期产出 LongLong ... |简短
输入 - LongLongLongLong | LongLongLongLong
预期产出 LongLong ... | LongLong ......
我试图创建一个自定义视图AutoSizingTextLayout(扩展LinearLayout),它包含两个自定义文本视图,初始相等的布局权重= 1.我为第二个文本视图添加了一个onDrawListener(右边一个)。第二个文本视图完成绘图后,我会根据截断的文本调整权重。
这种逻辑有效,但不可靠。在某些运行中,它运行完美,但大多数情况下,第一个文本视图占据整个屏幕。
我正在粘贴下面代码的相关部分。有什么需要处理的吗?有更优雅的解决方案吗? PS:此自定义视图正在列表回收站视图中使用。
public class AutoSizingTextLayout extends LinearLayout implements
AutoSizingTextView.AutoSizingTextViewDrawListener {
@BindView(R.id.start_text_view)
AutoSizingTextView mStartTextView;
@BindView(R.id.end_text_view)
AutoSizingTextView mEndTextView;
private volatile boolean adjusted = false;
//Constructors
@Override
public void onDraw() {
if (!adjusted) {
adjustWeights();
}
}
public void setStartViewText(String startText) {
mStartTextView.setText(startText);
}
public void setEndViewText(String endText) {
adjusted = false;
mEndTextView.setText(endText);
}
private void initView(AttributeSet attrs) {
LayoutInflater inflater = LayoutInflater.from(getContext());
inflater.inflate(R.layout.auto_sizing_text_layout, this);
ButterKnife.bind(this);
mEndTextView.setDrawListener(this);
}
private void adjustWeights() {
boolean startTextHasEllipses = hasEllipses(mStartTextView);
boolean endTextHasEllipses = hasEllipses(mEndTextView);
ArrayList<AutoSizingTextView> viewsToUpdate = new ArrayList<>();
if (startTextHasEllipses && endTextHasEllipses) {
//Both are truncated. Do nothing.
} else if (startTextHasEllipses) {
// Starting text is truncated. End text is not truncated. Give more weight to starting text.
viewsToUpdate.add(mEndTextView);
} else if (endTextHasEllipses) {
// Starting text is not truncated. End text is truncated. Give more weight to ending text.
viewsToUpdate.add(mStartTextView);
} else {
// Both are not truncated. Set it to wrap content.
viewsToUpdate.add(mStartTextView);
viewsToUpdate.add(mEndTextView);
}
updateView(viewsToUpdate);
}
private void updateView(List<AutoSizingTextView> viewsToUpdate) {
for (AutoSizingTextView viewToUpdate : viewsToUpdate) {
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)
viewToUpdate.getLayoutParams();
params.width = LinearLayout.LayoutParams.WRAP_CONTENT;
params.weight = 0.0F;
adjusted = true;
viewToUpdate.setLayoutParams(params);
}
this.requestLayout();
}
private static boolean hasEllipses(TextView textView) {
Layout layout = textView.getLayout();
if (layout != null) {
int lines = layout.getLineCount();
if (lines > 0) {
if (layout.getEllipsisCount(lines - 1) > 0) {
return true;
}
}
}
return false;
}
}
//自定义文字视图
class AutoSizingTextView extends
android.support.v7.widget.AppCompatTextView {
private AutoSizingTextViewDrawListener mDrawListener;
//Constructors
public void setDrawListener(AutoSizingTextViewDrawListener drawListener) {
mDrawListener = drawListener;
}
public interface AutoSizingTextViewDrawListener {
void onDraw();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mDrawListener != null) {
mDrawListener.onDraw();
}
}
}
// auto_sizing_text_layout.xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<AutoSizingTextView
android:id="@+id/start_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxLines="1"
android:ellipsize="end"/>
<AutoSizingTextView
android:id="@+id/end_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxLines="1"
android:ellipsize="end"/>
答案 0 :(得分:0)
我认为以下是预期的结果。
我为此写了一个简单的ViewGroup
。当然,它并不完美。它只是表明它是可以实现的。相应地修改此代码。我已经覆盖了ViewGroup
的{{3}}方法,我正在计算所需的子视图大小并相应地进行布局。此WeirdView
分别包含三个TextView
(左侧文本,分隔符和右侧文本)。
package com.example.abhinav.textviewexample;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* Created by abhinav on 16/04/18.
*/
public class WeirdView extends ViewGroup {
private TextView mLeftTV;
private TextView mSeparatorTV;
private TextView mRightTV;
public WeirdView(Context context) {
this(context, null, 0);
}
public WeirdView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public WeirdView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
mLeftTV = new TextView(context);
mSeparatorTV = new TextView(context);
mRightTV = new TextView(context);
mLeftTV.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
mSeparatorTV.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
mRightTV.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
addView(mLeftTV);
addView(mSeparatorTV);
addView(mRightTV);
}
public TextView getLeftTextView() {
return mLeftTV;
}
public TextView getSeparatorTextView() {
return mSeparatorTV;
}
public TextView getRightTextView() {
return mRightTV;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// super.onLayout(changed, l, t, r, b);
final int childLeft = this.getPaddingLeft();
final int childTop = this.getPaddingTop();
final int childRight = this.getMeasuredWidth() - this.getPaddingRight();
final int childBottom = this.getMeasuredHeight() - this.getPaddingBottom();
final int availableWidth = childRight - childLeft;
final int availableHeight = childBottom - childTop;
mLeftTV.measure(MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(availableHeight, MeasureSpec.AT_MOST));
mSeparatorTV.measure(MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(availableHeight, MeasureSpec.AT_MOST));
mRightTV.measure(MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(availableHeight, MeasureSpec.AT_MOST));
// case: |blabla > blablabla |
// case: |blablablablablabla > bla |
// case: |bla > blablablablablablab..|
if ((mLeftTV.getMeasuredWidth() + mSeparatorTV.getMeasuredWidth() + mRightTV.getMeasuredWidth() <= availableWidth)
|| (mLeftTV.getMeasuredWidth() <= availableWidth / 2)) {
int left = childLeft;
mLeftTV.layout(left, childTop, left + mLeftTV.getMeasuredWidth(), childBottom);
left += mLeftTV.getMeasuredWidth();
mSeparatorTV.layout(left, childTop, left + mSeparatorTV.getMeasuredWidth(), childBottom);
left += mSeparatorTV.getMeasuredWidth();
mRightTV.layout(left, childTop, childRight, childBottom);
mRightTV.setWidth(childRight - left);
}
// case: |blablablablablabla.. > blab|
else if (mRightTV.getMeasuredWidth() <= availableWidth / 2) {
int left = childLeft;
mLeftTV.layout(left, childTop, childRight - mSeparatorTV.getMeasuredWidth() - mRightTV.getMeasuredWidth(), childBottom);
mLeftTV.setWidth(childRight - mSeparatorTV.getMeasuredWidth() - mRightTV.getMeasuredWidth() - left);
left += mLeftTV.getWidth();
mSeparatorTV.layout(left, childTop, mSeparatorTV.getMeasuredWidth(), childBottom);
left += mSeparatorTV.getMeasuredWidth();
mRightTV.layout(left, childTop, mRightTV.getMeasuredWidth(), childBottom);
}
// case: |blablabla.. > blablablabl..|
else {
int left = childLeft;
mLeftTV.layout(left, childTop, left + availableWidth / 2 - mSeparatorTV.getMeasuredWidth() / 2, childBottom);
mLeftTV.setWidth(availableWidth / 2 - mSeparatorTV.getMeasuredWidth() / 2);
left += (availableWidth / 2 + mSeparatorTV.getMeasuredWidth() / 2);
mSeparatorTV.layout(left, childTop, left + mSeparatorTV.getMeasuredWidth(), childBottom);
left += mSeparatorTV.getMeasuredWidth();
mRightTV.layout(left, childTop, childRight, childBottom);
mRightTV.setWidth(childRight - left);
}
}
}