两张卡片视图显示相同的图标

时间:2015-11-28 13:52:59

标签: android xml android-cardview

我有以下xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:showIn="@layout/activity_main_screen"
    tools:context="com.example.ahmed.supermarketproject.MainScreen">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        card_view:cardCornerRadius="2dp"
        card_view:contentPadding="20dp"
        card_view:cardBackgroundColor="@color/cardview_light_background"
        android:onClick="start"
        >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <ImageView android:src="@drawable/new_product_icon"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_marginLeft="20dp"
                android:id="@+id/Register_icon"/>
            <TextView
                android:id="@+id/Register"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="New Product"
                android:layout_below="@+id/Register_icon"
                android:textStyle="bold"
                android:textSize="20dp" />

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

    <android.support.v7.widget.CardView
        android:id="@+id/card_view2"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@+id/card_view"
        card_view:cardCornerRadius="2dp"
        card_view:contentPadding="20dp"
        >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <ImageView android:src="@drawable/new_order"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_gravity="center"
                android:id="@+id/New_Order_icon"/>
            <TextView
                android:id="@+id/New_oreder"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="New Order"
                android:layout_below="@+id/New_Order_icon"
                android:textStyle="bold"
                android:textSize="20dp" />

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

</RelativeLayout>

在每个卡片视图中,我有一个imageView,你可以在这段代码中看到 在第一张卡片视图中,我有图像<ImageView android:src="@drawable/new_product_icon"

的src 在第二张卡片视图上的

我有图片<ImageView android:src="@drawable/new_order"

的src

问题是两张卡片视图显示相同的图像,正如您所看到的,我正在使用两个不同的图像 ,那会是什么问题?

以下是我的java代码,但我认为它没有任何问题:

public class MainScreen extends AppCompatActivity {
   CardView register;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_screen);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        register = (CardView)findViewById(R.id.card_view);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        register.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    int color = getResources().getColor(R.color.cardview_shadow_start_color);
                    register.setBackgroundColor(color);
                }
                if(event.getAction() == MotionEvent.ACTION_UP){
                    int color = getResources().getColor(R.color.cardview_light_background);
                    register.setBackgroundColor(color);
                }
                return false;
            }
        });
    }
    public void start(View v){
        Intent intent = new Intent(this,xyz.class);
        startActivity(intent);
    }

这是第一张图片,注意左边的图标
first 这是第二张照片 seconde

以及它在设备上的外观:

third

0 个答案:

没有答案