显示像Confide android app这样的文字

时间:2015-02-13 20:11:18

标签: java android xml

我需要像Confide应用程序一样创建文本显示。我尝试的是使用FlowLayout,但是我无法获得该行,以便我可以隐藏显示行。似乎有各种各样的选择,但有点困惑,不能完全认为wwat ...就像打破TextView并显示在ListView但我不知道如何形成线条并创建adapter

如果有人知道这件事,请帮助我。我试图在Google上搜索,但没有任何结果。

目前,我使用TextView显示FlowLayout并显示隐藏每个TextView

1 个答案:

答案 0 :(得分:1)

尝试创建像这样的自定义视图,以免将其称为CustomTextView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="3dp"
android:layout_marginTop="3dp"
android:background="#FF0000"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="3dp"
android:paddingBottom="3dp" >

<TextView
android:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:maxLines="1"
android:textColor="#FFFFFF" />

</LinearLayout>

java代码:

public class CustomTextView extends FrameLayout implements View.OnClickListener {

    public CustomTextView(Context context) {
        super(context);
        init(context);
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.custom_text_view, this);
        setOnClickListener(this);
        mLabelTextView = (TextView) findViewById(R.id.label);
    }

    @Override
    public void onClick(View v) {
        mLabelTextView.setVisibility(View.VISIBLE)
    }
}

要将此视图添加到容器视图,请尝试以下操作:

Display display = ((Activity) getContext()).getWindowManager().getDefaultDisplay();
mContainerView.removeAllViewsInLayout();
mContainerView.removeAllViews();
int maxWidth = display.getWidth() - 20;

LinearLayout.LayoutParams params;
LinearLayout newLL = new LinearLayout(getContext());
newLL.setLayoutParams(new LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));
newLL.setGravity(Gravity.LEFT);
newLL.setOrientation(LinearLayout.HORIZONTAL);

int widthSoFar = 0;

for (CustomTextView customTextView : CustomTextViewList) {

    LinearLayout LL = new LinearLayout(getContext());
    LL.setOrientation(LinearLayout.HORIZONTAL);
    LL.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
    LL.setLayoutParams(new ListView.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT));
    customTextView.measure(0, 0);
    params = new LinearLayout.LayoutParams(customTextView.getMeasuredWidth(), FrameLayout.LayoutParams.WRAP_CONTENT);
    LL.addView(customTextView, params);
    LL.measure(0, 0);
    widthSoFar += customTextView.getMeasuredWidth();
    if (widthSoFar >= maxWidth) {
        mContainerView.addView(newLL);

        newLL = new LinearLayout(getContext());
        newLL.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));
        newLL.setOrientation(LinearLayout.HORIZONTAL);
        newLL.setGravity(Gravity.LEFT);
        params = new LinearLayout.LayoutParams(LL.getMeasuredWidth(), LL.getMeasuredHeight());
        newLL.addView(LL, params);
        widthSoFar = LL.getMeasuredWidth();
    } else {
        newLL.addView(LL);
    }
}
mContainerView.addView(newLL);

CustomTextViewList示例(通过评论回答您的问题)

ArrayList<CustomTextView> CustomTextViewList = new  ArrayList<CustomTextView>()
CustomTextViewList.add(new CustomTextView(context));
CustomTextViewList.add(new CustomTextView(context));
CustomTextViewList.add(new CustomTextView(context));