如何强制 9 个补丁图像仅针对 RecyclerView 中需要更多 space 的视图进行缩放?
How to force 9 patch images to scale only for Views that require more space in RecyclerView?
我有一个 RecyclerView
,我用消息更新它,我已经将背景设置为 RecyclerView
内的 Textview
和 9-patch-image
。
现在,问题 是:当消息的长度很小时,9 个补丁图像运行良好,并且仅缩放到 space 文本占用,但是当一条消息包含具有许多字符的文本时,显然 9 个补丁图像缩放,但当它缩放时,它也会缩放 RecyclerView
.
中的所有先前元素
可能是什么原因造成的,如何解决?
这是我的适配器的代码:
CustomAdapter.class
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.FeedsViewHolder>
{
DataHolder d1 = new DataHolder();
public class FeedsViewHolder extends RecyclerView.ViewHolder
{
private TextView chat;
private Typeface face;
private Context mcontext;
FeedsViewHolder(View itemView)
{
super(itemView);
mcontext = itemView.getContext();
chat = (TextView)itemView.findViewById(R.id.chatMessage);
face = Typeface.createFromAsset(itemView.getContext().getAssets(), "Fonts/Roboto-Regular.ttf");
chat.setTypeface(face);
}
}
private static class DataHolder
{
List<Text> feeds;
}
CustomAdapter(List<Text> feeds)
{
this.d1.feeds = feeds;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public FeedsViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.chat_message, viewGroup, false);
FeedsViewHolder pvh = new FeedsViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(FeedsViewHolder feedViewHolder, int i)
{
RelativeLayout.LayoutParams params =
(RelativeLayout.LayoutParams)feedViewHolder.chat.getLayoutParams();
feedViewHolder.chat.setPadding(0,0,0,0);
if(d1.feeds.get(i).isMachineOrHuman())
{
params.addRule(RelativeLayout.ALIGN_PARENT_START);
feedViewHolder.chat.setLayoutParams(params);
feedViewHolder.chat.setBackground(ContextCompat.getDrawable(feedViewHolder.mcontext, R.drawable.user));
feedViewHolder.chat.setText(d1.feeds.get(i).getMessage());
}
else
{
params.addRule(RelativeLayout.ALIGN_PARENT_END);
feedViewHolder.chat.setLayoutParams(params);
feedViewHolder.chat.setBackground(ContextCompat.getDrawable(feedViewHolder.mcontext, R.drawable.ais));
feedViewHolder.chat.setText(d1.feeds.get(i).getMessage());
}
}
@Override
public int getItemCount()
{
if(d1.feeds!=null)
{
return d1.feeds.size();
}
else
{
return 0;
}
}
}
这是设置背景的地方:
feedViewHolder.chat.setBackground(ContextCompat.getDrawable(feedViewHolder.mcontext, R.drawable.user));
feedViewHolder.chat.setBackground(ContextCompat.getDrawable(feedViewHolder.mcontext, R.drawable.ais));
这是 XML
到适配器的 TextView
:
chat_message.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/chat_rel"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/chatMessage"
android:layout_width="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:gravity="start"
android:textSize="15sp"
android:textColor="#ffffff"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" />
EDIT-1
这是前后的样子:
IMAGE-1
IMAGE-2
显然,问题出在我定义重力的地方:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/chat_rel"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/chatMessage"
android:layout_width="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:gravity="start" <!-- here -->
android:textSize="15sp"
android:textColor="#ffffff"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" />
解决方法:
从 XML 中删除了 android:gravity="start"
并以编程方式定义它:
feedViewHolder.chat.setGravity(Gravity.START);
feedViewHolder.chat.setGravity(Gravity.END);
onBindViewHolder()
变为:
@Override
public void onBindViewHolder(FeedsViewHolder feedViewHolder, int i)
{
RelativeLayout.LayoutParams params =
(RelativeLayout.LayoutParams)feedViewHolder.chat.getLayoutParams();
if(d1.feeds.get(i).isMachineOrHuman())
{
params.addRule(RelativeLayout.ALIGN_PARENT_START);
feedViewHolder.chat.setLayoutParams(params);
feedViewHolder.chat.setGravity(Gravity.START);
feedViewHolder.chat.setBackground(ContextCompat.getDrawable(feedViewHolder.mcontext, R.drawable.user));
feedViewHolder.chat.setText(d1.feeds.get(i).getMessage());
}
else
{
params.addRule(RelativeLayout.ALIGN_PARENT_END);
feedViewHolder.chat.setLayoutParams(params);
feedViewHolder.chat.setGravity(Gravity.END);
feedViewHolder.chat.setBackground(ContextCompat.getDrawable(feedViewHolder.mcontext, R.drawable.ais));
feedViewHolder.chat.setText(d1.feeds.get(i).getMessage());
}
}
我有一个 RecyclerView
,我用消息更新它,我已经将背景设置为 RecyclerView
内的 Textview
和 9-patch-image
。
现在,问题 是:当消息的长度很小时,9 个补丁图像运行良好,并且仅缩放到 space 文本占用,但是当一条消息包含具有许多字符的文本时,显然 9 个补丁图像缩放,但当它缩放时,它也会缩放 RecyclerView
.
可能是什么原因造成的,如何解决?
这是我的适配器的代码:
CustomAdapter.class
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.FeedsViewHolder>
{
DataHolder d1 = new DataHolder();
public class FeedsViewHolder extends RecyclerView.ViewHolder
{
private TextView chat;
private Typeface face;
private Context mcontext;
FeedsViewHolder(View itemView)
{
super(itemView);
mcontext = itemView.getContext();
chat = (TextView)itemView.findViewById(R.id.chatMessage);
face = Typeface.createFromAsset(itemView.getContext().getAssets(), "Fonts/Roboto-Regular.ttf");
chat.setTypeface(face);
}
}
private static class DataHolder
{
List<Text> feeds;
}
CustomAdapter(List<Text> feeds)
{
this.d1.feeds = feeds;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public FeedsViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.chat_message, viewGroup, false);
FeedsViewHolder pvh = new FeedsViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(FeedsViewHolder feedViewHolder, int i)
{
RelativeLayout.LayoutParams params =
(RelativeLayout.LayoutParams)feedViewHolder.chat.getLayoutParams();
feedViewHolder.chat.setPadding(0,0,0,0);
if(d1.feeds.get(i).isMachineOrHuman())
{
params.addRule(RelativeLayout.ALIGN_PARENT_START);
feedViewHolder.chat.setLayoutParams(params);
feedViewHolder.chat.setBackground(ContextCompat.getDrawable(feedViewHolder.mcontext, R.drawable.user));
feedViewHolder.chat.setText(d1.feeds.get(i).getMessage());
}
else
{
params.addRule(RelativeLayout.ALIGN_PARENT_END);
feedViewHolder.chat.setLayoutParams(params);
feedViewHolder.chat.setBackground(ContextCompat.getDrawable(feedViewHolder.mcontext, R.drawable.ais));
feedViewHolder.chat.setText(d1.feeds.get(i).getMessage());
}
}
@Override
public int getItemCount()
{
if(d1.feeds!=null)
{
return d1.feeds.size();
}
else
{
return 0;
}
}
}
这是设置背景的地方:
feedViewHolder.chat.setBackground(ContextCompat.getDrawable(feedViewHolder.mcontext, R.drawable.user));
feedViewHolder.chat.setBackground(ContextCompat.getDrawable(feedViewHolder.mcontext, R.drawable.ais));
这是 XML
到适配器的 TextView
:
chat_message.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/chat_rel"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/chatMessage"
android:layout_width="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:gravity="start"
android:textSize="15sp"
android:textColor="#ffffff"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" />
EDIT-1
这是前后的样子:
IMAGE-1
IMAGE-2
显然,问题出在我定义重力的地方:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/chat_rel"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/chatMessage"
android:layout_width="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:gravity="start" <!-- here -->
android:textSize="15sp"
android:textColor="#ffffff"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" />
解决方法:
从 XML 中删除了 android:gravity="start"
并以编程方式定义它:
feedViewHolder.chat.setGravity(Gravity.START);
feedViewHolder.chat.setGravity(Gravity.END);
onBindViewHolder()
变为:
@Override
public void onBindViewHolder(FeedsViewHolder feedViewHolder, int i)
{
RelativeLayout.LayoutParams params =
(RelativeLayout.LayoutParams)feedViewHolder.chat.getLayoutParams();
if(d1.feeds.get(i).isMachineOrHuman())
{
params.addRule(RelativeLayout.ALIGN_PARENT_START);
feedViewHolder.chat.setLayoutParams(params);
feedViewHolder.chat.setGravity(Gravity.START);
feedViewHolder.chat.setBackground(ContextCompat.getDrawable(feedViewHolder.mcontext, R.drawable.user));
feedViewHolder.chat.setText(d1.feeds.get(i).getMessage());
}
else
{
params.addRule(RelativeLayout.ALIGN_PARENT_END);
feedViewHolder.chat.setLayoutParams(params);
feedViewHolder.chat.setGravity(Gravity.END);
feedViewHolder.chat.setBackground(ContextCompat.getDrawable(feedViewHolder.mcontext, R.drawable.ais));
feedViewHolder.chat.setText(d1.feeds.get(i).getMessage());
}
}