CardView调整长度和宽度以适应屏幕

时间:2016-12-15 17:45:55

标签: android android-layout android-fragments android-cardview

在我的应用程序中,我正在使用CardView UI元素。我有两个CardView元素,我想在UI中显示。这是我的代码。

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/card_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    card_view:cardUseCompatPadding="true"
    card_view:cardCornerRadius="8dp"
    android:layout_marginBottom="16dp">

    <LinearLayout android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/display_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="13sp"
            android:text="@string/app_name"
            android:textColor="#ffffff"
            android:paddingBottom="8dp"
            android:paddingTop="8dp"
            android:gravity="center_horizontal"
            android:layout_alignParentBottom="true"
            android:background="#1976D2"/>
        <ImageView
            android:id="@+id/display_image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:contentDescription="@string/action_settings"
            android:scaleType="centerCrop" />
        <TextView
            android:id="@+id/display_information"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="13sp"
            android:text="@string/app_name"
            android:textColor="#ffffff"
            android:layout_below="@+id/display_image"
            android:paddingBottom="8dp"
            android:paddingTop="8dp"
            android:gravity="center_horizontal"
            android:layout_alignParentBottom="true"
            android:background="#1976D2"/>
    </LinearLayout>
</android.support.v7.widget.CardView>

这是ViewHolder&amp;我正在使用的Adpater课程。

    public class DisplayItemRecyclerViewAdapter extends RecyclerView.Adapter<DisplayItemViewHolders> {

    private List<DisplayItemInformation> itemList;
    private Context context;

    public DisplayItemRecyclerViewAdapter(Context context, List<DisplayItemInformation> itemList) {
        this.itemList = itemList;
        this.context = context;
    }

    @Override
    public DisplayItemViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {

        View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.display_card, parent, false);
        layoutView.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.WRAP_CONTENT));
        DisplayItemViewHolders rcv = new DisplayItemViewHolders(layoutView);
        return rcv;
    }

    @Override
    public void onBindViewHolder(DisplayItemViewHolders holder, int position) {
        holder.name.setText(itemList.get(position).getName());
        holder.image.setImageResource(itemList.get(position).getPhoto());
    }

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


public class DisplayItemViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener {

    public TextView name;
    public TextView detailedInformation;
    public ImageView image;

    //private final Context context;

    public DisplayItemViewHolders(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);

        //context = itemView.getActivity();

        name = (TextView) itemView.findViewById(R.id.display_name);
        image = (ImageView) itemView.findViewById(R.id.display_image);
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(view.getContext(), importantDatesDashboardActivity.class);
        view.getContext().startActivity(intent);

        Toast.makeText(view.getContext(), "New ACTIVITY started 111 = " + getAdapterPosition(), Toast.LENGTH_SHORT).show();

    }
}

在上面的代码中,我想调整两张卡的宽度和高度以适应屏幕?我如何调整宽度和宽度卡的高度和垂直显示卡?这里要注意的是卡内显示的图像大于屏幕尺寸。

提前致谢, IamHuM

0 个答案:

没有答案