gpt4 book ai didi

java - Android CardView 填充错误

转载 作者:行者123 更新时间:2023-11-29 04:39:37 25 4
gpt4 key购买 nike

---TLDR---

我正在学习一个教程,其中在 cardview 和 recyclerview 的帮助下,我应该能够像这样在彼此下面列出文本:它应该是什么样子(不需要图像)

实际情况如何

---问题---

正如标题和图片所示,似乎 2 个 cardview 项目之间有一个巨大的填充,这显然不是我想要的。我希望它们彼此靠近,如另一张照片所示。

请记住,我是一个 super 新手程序员 :D。提前感谢您的所有回答。

---代码---

cards_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="cards main container">

<android.support.v7.widget.CardView
android:layout_width="300dp"
android:layout_height="60dp"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
card_view:cardElevation="5dp"
card_view:cardUseCompatPadding="true"
card_view:cardBackgroundColor="#FFF"
android:id="@+id/card_view"
android:layout_gravity="center_horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Marketing Tip"
android:id="@+id/textviewName"
android:textColor="#000"/>

</LinearLayout>

</android.support.v7.widget.CardView>

</LinearLayout>

HomeActivity.java

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import java.util.ArrayList;

public class HomeActivity extends AppCompatActivity {

private static RecyclerView.Adapter adapter;
private RecyclerView.LayoutManager layoutManager;
private static RecyclerView recyclerView;
private static ArrayList<DataModel> data;
static View.OnClickListener myOnClickListener;




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);

setTitle("Home");


myOnClickListener = new myOnClickListener(this);


recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
recyclerView.setHasFixedSize(true);

layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());

data = new ArrayList<DataModel>();
for (int i = 0; i < MyData.nameArray.length; i++) {
data.add(new DataModel(
MyData.nameArray[i],
MyData.id_[i]
));

adapter = new CustomAdapter(data);
recyclerView.setAdapter(adapter);

}

}

private static class myOnClickListener implements View.OnClickListener {

private final Context context;

private myOnClickListener(Context context) {
this.context = context;
}

@Override
public void onClick(View v) {
//TODO WRITE THE CODE TO START AN ACTIVITY ONCE THE CARDVIEW IS CLICKED
}
}

}

CustomAdapter.java

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MyViewHolder>{

private ArrayList<DataModel> dataSet;

public static class MyViewHolder extends RecyclerView.ViewHolder {

TextView textviewName;


public MyViewHolder(View itemView) {
super(itemView);
this.textviewName = (TextView)itemView.findViewById(R.id.textviewName);

}
}

public CustomAdapter(ArrayList<DataModel> data){
this.dataSet = data;

}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cards_layout, parent, false);
view.setOnClickListener(HomeActivity.myOnClickListener);

MyViewHolder myViewHolder = new MyViewHolder(view);

return myViewHolder;
}

@Override
public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {

TextView textviewName = holder.textviewName;

textviewName.setText(dataSet.get(listPosition).getName());


}

@Override
public int getItemCount() {
return dataSet.size();
}
}

DataModel.java

public class DataModel {
String name;
int id_;

public DataModel(String name, int id_) {

this.name = name;
this.id_ = id_;
}

public String getName() {
return name;
}

public int getId() {
return id_;
}
}

MyData.java

public class MyData {

static String[] nameArray = {"Buzi", "Kocsog", "Fostalicska", "CiganyTibi"};

static Integer[] id_ = {0, 1, 2, 3};
}

最佳答案

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="cards main container">

<android.support.v7.widget.CardView
android:layout_width="300dp"
android:layout_height="60dp"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
card_view:cardElevation="5dp"
card_view:cardUseCompatPadding="true"
card_view:cardBackgroundColor="#FFF"
android:id="@+id/card_view"
android:layout_gravity="center_horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:text="Marketing Tip"
android:id="@+id/textviewName"
android:textColor="#000"/>

</LinearLayout>

</android.support.v7.widget.CardView>

</LinearLayout>

将其用作您的卡片 View 布局。您基本上将 match_parent 用于线性布局,导致 View 填满整个屏幕。将其更改为 wrap_content 应该可以完成这项工作。休息就好。

关于java - Android CardView 填充错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39768308/

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