这是该片段的XML的一个片段:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="@drawable/bg_grey_radial"
android:weightSum="10"
android:paddingTop="10dp"
android:paddingBottom="45dp"
>
<GridLayout
android:id="@+id/mainGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="8"
android:alignmentMode="alignMargins"
android:columnCount="2"
android:columnOrderPreserved="false"
android:padding="14dp"
android:rowCount="3"
>
<!-- Row 1 -->
<!-- ITEM 1 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_rowWeight="1"
app:cardBackgroundColor="@color/zxing_transparent"
app:cardCornerRadius="8dp"
app:cardElevation="0dp"
android:id="@+id/cardview-item1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="10dp"
android:orientation="vertical"
android:weightSum="10">
<ImageView
android:id="@+id/icon1"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_gravity="center_horizontal"
android:layout_weight="8"
android:src="@drawable/ic_diamond"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:elegantTextHeight="true"
android:singleLine="true"
android:text="ITEM1"
android:textAlignment="center"
android:textColor="@color/text"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/icon7"
android:paddingTop="15dp"
android:lines="2"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Item 2 in Row 1 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_rowWeight="1"
app:cardBackgroundColor="@color/zxing_transparent"
app:cardCornerRadius="8dp"
app:cardElevation="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="10dp"
android:orientation="vertical"
android:weightSum="10">
<ImageView
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_gravity="center_horizontal"
android:layout_weight="8"
android:src="@drawable/ic_diamond" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:elegantTextHeight="true"
android:singleLine="true"
android:text="ITEM 2"
android:textAlignment="center"
android:textColor="@color/text"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/icon7"
android:paddingTop="15dp"
android:lines="2"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 2 -->
<!-- Item 1 in Row 2 -->
这不是完整的XML代码,正如您从屏幕快照中看到的那样,它会重复自身。
我试图在每个项目上设置一个OnClickListener,我也想在Java代码中分别更改每个项目的图标(后来在Picasso上加载了Image)。
public fragment() {
// Required empty public constructor
}
public static fragment newInstance(String param1, String param2) {
fragment = new fragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
mainGrid = view.findViewById(R.id.mainGrid);
//Set Event
CardView infobase = view.findViewById(R.id.cardview_item1);
infobase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), activity.class);
Log.e("Cardview","CLICK");
startActivity(intent);
}
});
ImageView icon1 = view.findViewById(R.id.icon1);
icon1.setImageResource(R.drawable.ic_bank);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment, container, false);
}
使用view.setOnClickListener设置OnClickListener无效,也不会更改Image。我没有在Logcat上收到任何事件,所以我猜Java无法正确访问CardView。这不是访问片段中CardView项的正确方法吗?
答案 0 :(得分:2)
您应该改为path {
cursor: pointer;
fill: #00338D;
stroke: #000;
stroke-width: 1;
}
return view
这是更新的代码行
return inflater.inflate(R.layout.fragment, container, false);
答案 1 :(得分:0)
在xml的CardView ID中,您使用了破折号,但是您的Java代码使用了下划线。将android:id =“ @ + id / cardview-item1”更改为android:id =“ @ + id / cardview_item1”