我正在尝试在ReViewView中的CardView中的TextView上动态设置maxLines。
这是我的CardView_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<android.support.v7.widget.CardView
android:id="@+id/fragment_advice_cardview_CV_cardview"
android:layout_width="match_parent"
android:layout_height="150sp"
card_view:cardCornerRadius="5sp"
card_view:cardElevation="5sp"
card_view:contentPadding="2dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/green_background"
android:padding="16sp">
<ImageView
android:id="@+id/fragment_advice_cardview_IV_doctor_photo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="16sp"
android:adjustViewBounds="true"
android:maxWidth="150sp"
android:scaleType="fitCenter"
android:src="@drawable/batman_logo" />
<TextView
android:id="@+id/fragment_advice_cardview_TV_doctor_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/fragment_advice_cardview_IV_doctor_photo"
android:text="@string/omg"
android:textSize="30sp" />
<TextView
android:id="@+id/fragment_advice_cardview_TV_doctor_advice"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/fragment_advice_cardview_TV_doctor_name"
android:layout_toEndOf="@+id/fragment_advice_cardview_IV_doctor_photo"
android:text="@string/omg" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
这是我的fragment_layout.xml:
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_advice_VS_view_switcher"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".android.fragments.AdviceFragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/fragment_advice_RV_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/fragment_advice_TV_empty_error_message"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</ViewSwitcher>
我的适配器:
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.DoctorViewHolder> {
private final ArrayList<Doctor> doctors;
public RVAdapter(ArrayList<Doctor> doctors) {
this.doctors = doctors;
}
@Override
public DoctorViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_advice_cardview_layout, parent, false);
final TextView doctorAdviceTV = (TextView) view.findViewById(R.id.fragment_advice_cardview_TV_doctor_advice);
System.out.println("doctorAdviceTV.getId() : " + doctorAdviceTV.getId());
System.out.println("doctorAdviceTV.getHeight() : " + doctorAdviceTV.getHeight());
ViewTreeObserver doctorAdviceTVviewTreeObserver = doctorAdviceTV.getViewTreeObserver();
doctorAdviceTVviewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
doctorAdviceTV.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int maxLines = doctorAdviceTV.getHeight() / doctorAdviceTV.getLineHeight();
BaseActivity.logD("RVAdapter", "maxLines : " + maxLines + " = " + doctorAdviceTV.getHeight() + " / " + doctorAdviceTV.getLineHeight());
doctorAdviceTV.setMaxLines(maxLines);
doctorAdviceTV.setEllipsize(TextUtils.TruncateAt.END);
}
});
DoctorViewHolder dvh = new DoctorViewHolder(view);
return dvh;
}
@Override
public void onBindViewHolder(DoctorViewHolder holder, int position) {
Doctor doctor = doctors.get(position);
holder.doctorNameTV.setText(doctor.getSettingValue(Doctor.SETTINGS.NAME));
holder.doctorAdviceTV.setText(doctor.getSettingValue(Doctor.SETTINGS.ADVICE));
Bitmap profilePicture = doctor.getProfilePicture();
if (profilePicture == null) {
holder.doctorImageIV.setImageResource(R.drawable.batman_logo);
} else {
holder.doctorImageIV.setImageBitmap(profilePicture);
}
}
@Override
public int getItemCount() {
return doctors.size();
}
public static class DoctorViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView doctorNameTV;
TextView doctorAdviceTV;
ImageView doctorImageIV;
public DoctorViewHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.fragment_advice_cardview_CV_cardview);
doctorNameTV = (TextView) itemView.findViewById(R.id.fragment_advice_cardview_TV_doctor_name);
doctorAdviceTV = (TextView) itemView.findViewById(R.id.fragment_advice_cardview_TV_doctor_advice);
doctorImageIV = (ImageView) itemView.findViewById(R.id.fragment_advice_cardview_IV_doctor_photo);
}
}
}
当然结果
在改变之前:
正如您所看到的,第一个CardView TextView正确更新到正确的行数,但第二个仍保持1,即使有更多的空间。
请帮忙。
谢谢
答案 0 :(得分:1)
尝试
textView.setMaxLines(("This is a \n \n test".split("\n").length+1));