毕加索:为什么我的卡片视图在图像加载时改变了尺寸?
Picasso: Why is my cardview changes its dimension when the image load?
我在加载图片时使用了 Picasso,我的问题是我的卡片视图在加载图片时改变了它的维度。看起来像这样;
image(1) loads and looks like this after the Image(2)
之前
如何在图像加载时固定尺寸。这是我的代码
drawer_layout_list.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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="128dp"
card_view:cardBackgroundColor="@android:color/transparent"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="3dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="2">
<ImageView
android:id="@+id/partner_post_image"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_margin="8dp" />
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:weightSum="3"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/partner_store"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:gravity="bottom|start"
android:text="Store Name"
android:textAlignment="viewStart"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/partner_post_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_weight="2"
android:gravity="top|start"
android:text="Description Here"
android:textAlignment="viewStart"
android:textColor="#000"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
我的RecyclerView.Holder代码是这样的
public class PartnerStoreViewHolder extends RecyclerView.ViewHolder {
private View mView;
public PartnerStoreViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setStore(String store ){
TextView storeName = (TextView) mView.findViewById(R.id.partner_store);
storeName.setText(store);
}
public void setDescription(String description){
TextView descName = (TextView) mView.findViewById(R.id.partner_post_desc);
descName.setText(description);
}
public void setImage(Context context, String image){
ImageView postImage = (ImageView) mView.findViewById(R.id.partner_post_image);
Picasso.with(context)
.load(image)
.error(android.R.drawable.stat_notify_error)
.placeholder(R.drawable.ic_blu_logo)
.fit()
.into(postImage);
}
}
我的片段代码和适配器
public class PartnerStore extends BaseFragment {
private RecyclerView recyclerView;
private FirebaseRecyclerAdapter<PartnersList, PartnerStoreViewHolder> firebaseRecyclerAdapter;
private DatabaseReference mRef;
public PartnerStore() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.drawer_partner_store, container, false);
mRef = MyDatabaseUtil.getDatabase().getReference().child("partners");
recyclerView = (RecyclerView) view.findViewById(R.id.partner_store_recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return view;
}
@Override
public void onStart() {
super.onStart();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<PartnersList, PartnerStoreViewHolder>(
PartnersList.class,
R.layout.drawer_partner_store_list,
PartnerStoreViewHolder.class,
mRef
){
@Override
protected void populateViewHolder(PartnerStoreViewHolder viewHolder, PartnersList model, int position) {
viewHolder.setStore(model.getPartnerStore());
viewHolder.setDescription(model.getDescription());
viewHolder.setImage(getActivity() ,model.getImage());
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
@Override
public void onDetach() {
super.onDetach();
firebaseRecyclerAdapter.cleanup();
}
}
谢谢..
更新
我的问题是我从来没有在我的 Texview 中使用过 space,虽然我仍然不知道为什么它会改变维度。好吧,我在我的 textview 上作弊,我添加了白色 spaces 以使用我的 textview 中未使用的 spaced 我实现它就像这样......
public void setDescription(String description){
TextView descName = (TextView) mView.findViewById(R.id.partner_post_desc);
if (description.length() <= 30){
for (int x=0; x <= 99; x++){
description = description + " "; //adding white spaces
}
descName.setText(description);
}else {
descName.setText(description);
}
}
感谢所有帮助..我很感激
试试这个方法。
Picasso.with(context)
.load(image)
.error(android.R.drawable.stat_notify_error)
.placeholder(R.drawable.ic_blu_logo)
.fit()
.centerCrop()
.into(postImage);
在 xml ImageView 中使用 android:scaleType="fitXY"
。
您是否在 ImageView 中尝试 android:layout_width="match_parent"
?
您只将视图的第三部分提供给文本视图,将另外两部分提供给图像视图的父布局,由于图像视图宽度固定,因此未使用。您根本不应该使用重量,因为您为 imageview 提供固定的宽度和高度并为 textview 包装内容。
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/partner_post_image"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_margin="8dp" />
<LinearLayout
android:weightSum="3"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/partner_store"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:gravity="bottom|start"
android:text="Store Name"
android:textAlignment="viewStart"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/partner_post_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_weight="2"
android:gravity="top|start"
android:text="Description Here"
android:textAlignment="viewStart"
android:textColor="#000"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
然后尝试
Picasso.with(context)
.load(image)
.error(android.R.drawable.stat_notify_error)
.placeholder(R.drawable.ic_blu_logo)
.fit()
.centerCrop()
.into(postImage);
如果您还有任何问题,请告诉我。
我在加载图片时使用了 Picasso,我的问题是我的卡片视图在加载图片时改变了它的维度。看起来像这样;
image(1) loads and looks like this after the Image(2)
之前
如何在图像加载时固定尺寸。这是我的代码
drawer_layout_list.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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="128dp"
card_view:cardBackgroundColor="@android:color/transparent"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="3dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="2">
<ImageView
android:id="@+id/partner_post_image"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_margin="8dp" />
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:weightSum="3"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/partner_store"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:gravity="bottom|start"
android:text="Store Name"
android:textAlignment="viewStart"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/partner_post_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_weight="2"
android:gravity="top|start"
android:text="Description Here"
android:textAlignment="viewStart"
android:textColor="#000"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
我的RecyclerView.Holder代码是这样的
public class PartnerStoreViewHolder extends RecyclerView.ViewHolder {
private View mView;
public PartnerStoreViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setStore(String store ){
TextView storeName = (TextView) mView.findViewById(R.id.partner_store);
storeName.setText(store);
}
public void setDescription(String description){
TextView descName = (TextView) mView.findViewById(R.id.partner_post_desc);
descName.setText(description);
}
public void setImage(Context context, String image){
ImageView postImage = (ImageView) mView.findViewById(R.id.partner_post_image);
Picasso.with(context)
.load(image)
.error(android.R.drawable.stat_notify_error)
.placeholder(R.drawable.ic_blu_logo)
.fit()
.into(postImage);
}
}
我的片段代码和适配器
public class PartnerStore extends BaseFragment {
private RecyclerView recyclerView;
private FirebaseRecyclerAdapter<PartnersList, PartnerStoreViewHolder> firebaseRecyclerAdapter;
private DatabaseReference mRef;
public PartnerStore() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.drawer_partner_store, container, false);
mRef = MyDatabaseUtil.getDatabase().getReference().child("partners");
recyclerView = (RecyclerView) view.findViewById(R.id.partner_store_recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return view;
}
@Override
public void onStart() {
super.onStart();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<PartnersList, PartnerStoreViewHolder>(
PartnersList.class,
R.layout.drawer_partner_store_list,
PartnerStoreViewHolder.class,
mRef
){
@Override
protected void populateViewHolder(PartnerStoreViewHolder viewHolder, PartnersList model, int position) {
viewHolder.setStore(model.getPartnerStore());
viewHolder.setDescription(model.getDescription());
viewHolder.setImage(getActivity() ,model.getImage());
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
@Override
public void onDetach() {
super.onDetach();
firebaseRecyclerAdapter.cleanup();
}
}
谢谢..
更新
我的问题是我从来没有在我的 Texview 中使用过 space,虽然我仍然不知道为什么它会改变维度。好吧,我在我的 textview 上作弊,我添加了白色 spaces 以使用我的 textview 中未使用的 spaced 我实现它就像这样......
public void setDescription(String description){
TextView descName = (TextView) mView.findViewById(R.id.partner_post_desc);
if (description.length() <= 30){
for (int x=0; x <= 99; x++){
description = description + " "; //adding white spaces
}
descName.setText(description);
}else {
descName.setText(description);
}
}
感谢所有帮助..我很感激
试试这个方法。
Picasso.with(context)
.load(image)
.error(android.R.drawable.stat_notify_error)
.placeholder(R.drawable.ic_blu_logo)
.fit()
.centerCrop()
.into(postImage);
在 xml ImageView 中使用 android:scaleType="fitXY"
。
您是否在 ImageView 中尝试 android:layout_width="match_parent"
?
您只将视图的第三部分提供给文本视图,将另外两部分提供给图像视图的父布局,由于图像视图宽度固定,因此未使用。您根本不应该使用重量,因为您为 imageview 提供固定的宽度和高度并为 textview 包装内容。
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/partner_post_image"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_margin="8dp" />
<LinearLayout
android:weightSum="3"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/partner_store"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:gravity="bottom|start"
android:text="Store Name"
android:textAlignment="viewStart"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/partner_post_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_weight="2"
android:gravity="top|start"
android:text="Description Here"
android:textAlignment="viewStart"
android:textColor="#000"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
然后尝试
Picasso.with(context)
.load(image)
.error(android.R.drawable.stat_notify_error)
.placeholder(R.drawable.ic_blu_logo)
.fit()
.centerCrop()
.into(postImage);
如果您还有任何问题,请告诉我。