gpt4 book ai didi

java - 显示类似 Confide android 应用程序的文本

转载 作者:行者123 更新时间:2023-12-01 11:55:15 28 4
gpt4 key购买 nike

我需要创建像 Confide 应用程序一样的文本显示。我尝试的是使用 FlowLayout 但随后我无法获取该行,以便我可以隐藏显示行。似乎有多种选择,但有点困惑,无法思考 wwat 到底要做什么...比如打破 TextView 并在 ListView 中显示,但我不知道如何形成线条并创建适配器

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

目前,我正在使用 FlowLayout 展示 TextView 并展示隐藏每个 TextView

最佳答案

尝试创建像这样的自定义 View ,以免将其称为 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)
}
}

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

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));

关于java - 显示类似 Confide android 应用程序的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28507956/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com