Java 毕加索:为什么我的cardview在图像加载时会改变尺寸?
我用毕加索加载我的图像,我的问题是我的cardview,当图像加载时,它会改变尺寸。看起来是这样的,Java 毕加索:为什么我的cardview在图像加载时会改变尺寸?,java,android,picasso,Java,Android,Picasso,我用毕加索加载我的图像,我的问题是我的cardview,当图像加载时,它会改变尺寸。看起来是这样的, 在加载之前,在加载之后看起来是这样的 如何在图像加载时固定尺寸。这是我的代码 抽屉布局列表.xml <?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xm
在加载之前,在加载之后看起来是这样的
如何在图像加载时固定尺寸。这是我的代码
抽屉布局列表.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>
我的片段代码和适配器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();
}
}
公共类PartnerStore扩展了BaseFragment{
私人回收站;
私人FirebaseRecyclerAdapter FirebaseRecyclerAdapter;
专用数据库参考mRef;
公共合伙商店(){
//必需的空公共构造函数
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(R.layout.drawer\u partner\u store,container,false);
mRef=MyDatabaseUtil.getDatabase().getReference().child(“合作伙伴”);
recyclerView=(recyclerView)视图.findViewById(R.id.partner\u store\u recycler\u视图);
recyclerView.setHasFixedSize(true);
setLayoutManager(新的LinearLayoutManager(getActivity());
返回视图;
}
@凌驾
public void onStart(){
super.onStart();
firebaseRecyclerAdapter=新的firebaseRecyclerAdapter(
PartnersList.class,
R.layout.drawer\u partner\u store\u list,
PartnerStoreViewHolder.class,
mRef
){
@凌驾
受保护的void populateViewHolder(PartnerStoreViewHolder viewHolder、PartnersList模型、int位置){
viewHolder.setStore(model.getPartnerStore());
viewHolder.setDescription(model.getDescription());
setImage(getActivity(),model.getImage());
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
@凌驾
公共无效连接(){
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);
}
}
public void setDescription(字符串描述){
TextView descName=(TextView)mView.findviewbyd(R.id.partner\u post\u desc);
如果(description.length()尝试这种方法
Picasso.with(context)
.load(image)
.error(android.R.drawable.stat_notify_error)
.placeholder(R.drawable.ic_blu_logo)
.fit()
.centerCrop()
.into(postImage);
在xml图像视图中使用android:scaleType=“fitXY”
。
你试过android:layout\u width=“match\u parent”吗在您的ImageView内部?您只将视图的三分之一提供给TextView,另外两部分提供给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>
如果您还有任何问题,请告诉我。android:layout\u width=“match\u parent”您的图像视图是的,先生,我尝试过了,但仍然没有解决问题。您可以发布onCreateViewHolder()吗这里的方法?@KunalChawla这是我从视图持有者那里得到的所有信息,我使用firebase recylerview库来实现这一点。好的。你能尝试添加属性
android:adjustViewBounds=“true”吗
到您的图像视图
,然后看看它是否能像您所想的那样工作?我明白您的意思了,先生,谢谢,我预计数据可能很少或很多..我填了很多文本..它能像我所想的那样工作..但是我想到的是..如果我只有很少的文本..我怎么能使我的视图保持不变?你有什么建议吗..好的y如果你有少量文本,你想做什么?将文本居中还是什么?只需向左对齐你可以通过检查文本长度是否小于你的范围,将textview父布局的重心设置为向左。明白了吗?先生,还有一件事,为什么图像加载后它会改变..如果你不介意查看我的帖子..图像1似乎做得很好h位置保持架..但在加载时将cardview推到左侧?
<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);