我试图将卡片视图集中在导航视图中,但它只是不想工作。在设计器中它看起来不错,但是当我在模拟器中运行它时它只是粘在左边。
卡片视图:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
app:strokeColor="@color/black"
app:strokeWidth="1dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/logo"
android:layout_width="100dp"
android:layout_height="75dp"
android:layout_gravity="center_horizontal"
android:contentDescription="null"
android:scaleType="centerInside"
android:src="@drawable/_13" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/naziv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="naziv"
android:textAppearance="?attr/textAppearanceHeadline6" />
<TextView
android:id="@+id/naslov"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="4dp"
android:layout_marginBottom="6dp"
android:text="naslov"
android:textAppearance="?attr/textAppearanceBody2"
android:textColor="?android:attr/textColorSecondary" />
<LinearLayout
android:id="@+id/gorivoLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<TableLayout
android:id="@+id/_95"
android:layout_width="150dp"
android:layout_height="wrap_content">
<TableRow
android:id="@+id/row1"
android:layout_height="wrap_content">
<TextView
android:id="@+id/_95Ime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="#b0b0b0"
android:padding="8dp"
android:text="Bencin 95"
android:textColor="#000"
android:textSize="8sp" />
</TableRow>
<TableRow
android:id="@+id/row2"
android:layout_height="wrap_content">
<TextView
android:id="@+id/_95Cena"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="#FF0000"
android:padding="8dp"
android:text="CENA"
android:textColor="#000"
android:textSize="8sp" />
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
主视图:
<?xml version="1.0" encoding="utf-8"?>
<!--the root view must be the DrawerLayout-->
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerLayout_najdene_bencinske_crpalke"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!--google maps-->
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" >
</fragment>
<!--this the navigation view which draws
and shows the navigation drawer-->
<!--include the menu created in the menu folder
-->
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:id="@+id/navigationView_najdene_bencinske_crpalke" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView_najdene_bencinske_crpalke"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
创建卡片
package com.example.bencinskecrpalke;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
/*
* Adapter Class in RecyclerView will get the data from your Modal Class and set that data to your item of RecyclerView.
* Below is the code for the MenuCardAdapter.java file. Comments are added inside the code to understand the code in more detail.
* */
public class MenuCardAdapter extends RecyclerView.Adapter<MenuCardAdapter.Viewholder> {
private Context context;
private ArrayList<MenuCardBencinskaCrpalka> menuCardsArrayList;
// Constructor
public MenuCardAdapter(Context context, ArrayList<MenuCardBencinskaCrpalka> menuCardsArrayList) {
this.context = context;
this.menuCardsArrayList = menuCardsArrayList;
}
@NonNull
@Override
public MenuCardAdapter.Viewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// to inflate the layout for each item of recycler view.
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.menu_card_layout, parent, false);
return new Viewholder(view);
}
@Override
public void onBindViewHolder(@NonNull MenuCardAdapter.Viewholder holder, int position) {
//se klice, ko se card dinamicno ustvari
// to set data to textview of each card layout
MenuCardBencinskaCrpalka model = menuCardsArrayList.get(position);
//holder je sestavljen glede menu_card_layout.xml
//model je objekt razreda MenuCardBencinskaCrpalka
holder.logo.setImageResource(model.getLogoImageID());
holder.naslov.setText(model.getNaslov());
holder.naziv.setText(model.getNaziv());
// holder.gorivoLayout = model.getGorivoLayout();
//Toast.makeText(holder.logo.getContext(), holder.naziv.getText(), Toast.LENGTH_SHORT).show();
}
@Override
public int getItemCount() {
// this method is used for showing number
// of card items in recycler view.
return menuCardsArrayList.size();
}
// View holder class for initializing of
// your views such as TextView and Imageview.
public class Viewholder extends RecyclerView.ViewHolder {
public View view; //trenutni CardView
public ImageView logo;
public TextView naziv, naslov;
public LinearLayout gorivoLayout;
public Viewholder(@NonNull View itemView) {
super(itemView);
logo = itemView.findViewById(R.id.logo);
naziv = itemView.findViewById(R.id.naziv);
naslov = itemView.findViewById(R.id.naslov);
//gorivoLayout = itemView.findViewById(R.id.gorivoLayout);
//Toast.makeText(itemView.getContext(), "test", Toast.LENGTH_SHORT).show();
view = itemView;
view.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
// item clicked
//Toast.makeText(v.getContext(), "clicked", Toast.LENGTH_SHORT).show();
}
});
}
}
}
有任何想法吗?