gpt4 book ai didi

java - picasso :为什么我的卡片 View 在图像加载时改变了尺寸?

转载 作者:行者123 更新时间:2023-11-30 10:29:07 25 4
gpt4 key购买 nike

我在加载图片时使用了 Picasso,我的问题是我的卡片 View 在加载图片时改变了它的维度。看起来像这样;
之前 image(1) Image(2) 之后加载并看起来像这样
如何在图像加载时固定尺寸。这是我的代码

抽屉布局列表.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);
}
}

我的 fragment 代码和适配器

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 中使用过空间,虽然我仍然不知道为什么它会改变尺寸。好吧,我在我的 textview 上作弊,我添加了空格以使用我的 textview 中未使用的间隔我实现它是这样的......

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);
}
}

感谢所有帮助...我很感激

最佳答案

您只将 View 的第三部分提供给 textviews,将另外两部分提供给 imageview 的父布局,由于 imageview 宽度是固定的,因此未使用。您根本不应该使用重量,因为您为 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);

如果您还有任何问题,请告诉我。

关于java - picasso :为什么我的卡片 View 在图像加载时改变了尺寸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44238043/

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