你好,我想根据InfoGenBibliotheque类中有多少个字符串列出cardView。我真的不知道如何基于项目计数创建cardViews列表。如果有人可以提供帮助,我将不胜感激。我已经读到我必须创建一个cardview布局文件才能从中创建一个列表。 //编辑 所以我设法使用listView和BaseAdapter使其成功。但是,当我执行应用程序时,在哪里找不到边距。
InfoGenBibliotheque类:
JavaVMInitArgs vm_args;
JavaVMOption* options = new JavaVMOption[4];
options[0].optionString = "-Djava.class.path=run.jar";
options[1].optionString = "-Djava.home=jre";
options[2].optionString = "-Xms1m"; // 1MB
options[3].optionString = "-Xmx1g"; // 1GB
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 3;
vm_args.options = options;
vm_args.ignoreUnrecognized = false;
JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
我的XML activity_info_generales文件:
public class InfoGenBibliotheque {
private String SalutaionsAnglais [] = {
"Certainly!",
"Good afternoon.",
"Good evening sir.",
"Good Luck.",
"Good morning."
};
private String SalutaionsFrancais [] = {
"Certainement!",
"Bon après-midi.",
"Bonsoir Monsieur.",
"Bonne chance.",
"Bonjour."
};
}
我的cardView xml文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".ui.InfoGeneralesActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_margin="10dp">
</ListView>
</android.support.constraint.ConstraintLayout>
适配器Java文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
android:id="@+id/expressionCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="center"
app:cardPreventCornerOverlap="false"
app:cardCornerRadius="8dp"
android:padding="-4dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:weightSum="2"
android:orientation="vertical"
android:gravity="center"
android:padding="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/colorPrimaryDark">
<TextView
android:id="@+id/expressionFrancais"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorAccent"
android:text="Najb Mohammed"
android:textSize="24dp"
android:textAlignment="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/backgroundColor">
<TextView
android:id="@+id/expressionAnglais"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Najb mohammed"
android:textSize="24dp"
android:textAlignment="center"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
填充表达式列表:
public class ExpressionAdapter extends BaseAdapter {
private Context context;
private ArrayList<Expression> expressions;
public ExpressionAdapter(Context context, ArrayList<Expression> expressions) {
this.context = context;
this.expressions = expressions;
}
@Override
public int getCount() {
return expressions.size();
}
@Override
public Object getItem(int position) {
return expressions.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = View.inflate(context, R.layout.expressions_card_view,null);
}
TextView expressionFrancais = convertView.findViewById(R.id.expressionFrancais);
TextView expressionAnglais = convertView.findViewById(R.id.expressionAnglais);
Expression expression = expressions.get(position);
expressionFrancais.setText(expression.getExpressionFrancais());
expressionAnglais.setText(expression.getExpressionAnglais());
return convertView;
}
}
表达类
public class ListExpression {
public static ArrayList<Expression> getExpressionList(){
ArrayList<Expression> expressionList = new ArrayList<>();
InfoGenBibliotheque infoGenBibliotheque = new InfoGenBibliotheque();
for(int i = 0;i < infoGenBibliotheque.getSalutaionsAnglais().length;i++){
expressionList.add(new Expression(i,infoGenBibliotheque.getSalutationFrancais(i),infoGenBibliotheque.getSalutationAnglais(i)));
}
return expressionList;
}
}
答案 0 :(得分:0)
只需使用CardView
作为根元素,边距就会消失:
将CardView
放在FrameLayout
内可以为您提供边距:
(但是我个人并不认为水平分隔线在卡片上看起来不错。如果您希望它们不可见,请看一下this post或使用RecyclerView
而不是ListView
。)
第二种方法的XML:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="@+id/expressionCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
app:cardCornerRadius="8dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:weightSum="2"
android:orientation="vertical"
android:gravity="center"
android:padding="4dp">
<!-- skipped inner Views here -->
</LinearLayout>
</android.support.v7.widget.CardView>
</FrameLayout>