gpt4 book ai didi

java - 自定义 TextView 不显示文本但仅显示背景

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:13:47 26 4
gpt4 key购买 nike

我有一个 TextView 的奇怪行为它打败了我我怎样才能找到解决这个问题的方法。我有一个布局

<?xml version='1.0' encoding='utf-8' ?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start"
android:orientation="horizontal"
android:visibility="visible">

<View
android:id="@+id/them_avatar_spacer"
android:layout_width="@dimen/avatar_size"
android:layout_height="0.0dip"
android:visibility="gone"/>

<org.slabs.fc.chatstarter.ui.CircularImageView
android:id="@+id/them_avatar"
android:layout_width="@dimen/avatar_size"
android:layout_height="@dimen/avatar_size"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:src="@drawable/default_avatar"
android:visibility="gone"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="4.0dip"
android:gravity="start"
android:orientation="vertical">

<TextView
android:id="@+id/chat_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2.0dip"
android:layout_marginLeft="6.0dip"
android:layout_marginTop="6dp"
android:includeFontPadding="false"
android:text="Title"
android:textColor="@color/black_light"
android:textSize="@dimen/chat_name_fontsize"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start"
android:orientation="horizontal">

<TextView
android:id="@+id/chat_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="63.0dip"
android:autoLink="all"
android:background="@drawable/selectable_balloon_left"
android:clickable="true"
android:lineSpacingExtra="@dimen/text_body_line_spacing"
android:linksClickable="true"
android:text="Every message text comes here"
android:textColor="@color/black_heavy"
android:textIsSelectable="false"
android:textSize="@dimen/chat_text_msg_fontsize"
android:visibility="visible"/>

<TextView
android:id="@+id/chat_time"
android:layout_width="60.0dip"
android:layout_height="wrap_content"
android:layout_gravity="start|center"
android:layout_marginLeft="-60.0dip"
android:text="12:15"
android:textColor="@color/black_lightest"
android:textSize="@dimen/chat_timestamp_fontsize"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>

它在 XML 编辑器的设计选项中为我提供了所需的布局,如图所示

desired layout

但是当我运行应用程序时,id 为 chat_text 的 TextView 没有显示,我得到如下结果,

weird results

然后我尝试创建一个自定义 TextView 我只是创建了一个

public class ChatThemTextView extends TextView {

public ChatThemTextView(Context context) {
super(context);
}

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

public ChatThemTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
ViewGroup.MarginLayoutParams margins = ViewGroup.MarginLayoutParams.class.cast(getLayoutParams());
int margin = 5;
margins.topMargin = margin;
margins.bottomMargin = margin;
margins.leftMargin = margin;
margins.rightMargin = margin;
setLayoutParams(margins);
}
}

此时出现了另一个问题,即当我添加第一条消息时我没有得到任何东西,但是当我添加第二条消息时我得到了背景,但 TextView 仍然没有显示,如下所示

enter image description here

你们都看到了预期的输出结果,感谢任何帮助......

更新

ViewHolder 很简单

public class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View itemView) {
super(itemView);
}
}

适配器类在这里

public class ChatMessageAdapter extends RecyclerView.Adapter<ViewHolder> {

private List<ChatMessage> mMessageList;
private Context mContext;
private boolean isMe = false;
public ChatMessageAdapter(Context mContext, List<ChatMessage> mMessageList){
this.mContext = mContext;
this.mMessageList = mMessageList;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View rootView = View.inflate(mContext, R.layout.chat_them_container, null);
return new ThemMessageHolder(rootView);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ThemMessageHolder messageHolder = (ThemMessageHolder) holder;
messageHolder.mMessageTextView.setText(mMessageList.get(position).getMessage());
messageHolder.mSentAtTextView.setText(mMessageList.get(position).getSentAt());
Log.e("MSG_TEXT", mMessageList.get(position).getMessage());
}

@Override
public int getItemCount() {
return mMessageList.size();
}

public void clearData() {
mMessageList.clear(); //clear list
this.notifyDataSetChanged();
}
}

重要

我在

得到 Sting 值
Log.e("MSG_TEXT", mMessageList.get(position).getMessage());

但是 TextView 没有显示那个字符串...

.gitignore 是

*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures

settings.gradle 只是

include ':app'

最佳答案

您可以更改:

View rootView = View.inflate(mContext, R.layout.chat_them_container, null);

收件人:

View rootView = LayoutInflater.from(mContext).inflate(R.layout.chat_them_container, parent, false);

它将尊重其父级的 LayoutParams,从而解决您的问题。

关于java - 自定义 TextView 不显示文本但仅显示背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41216507/

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