我有一个奇怪的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编辑器的设计选项中为我提供了所需的布局,如图所示
但是当我运行应用程序时,标识为chat_text
的TextView未显示,我得到如下结果,
然后我尝试创建一个自定义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仍未显示如此处所示
你们都看到了输出的预期,任何帮助都值得赞赏...
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没有显示String ...
.gitignore是
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
和settings.gradle只是
include ':app'
答案 0 :(得分:2)
您可以更改:
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
,从而解决您的问题。