0

我在使用这个布局时遇到了问题,它是我的网格适配器中的一个项目。

我正在使用 caldroid

我在 getview 中膨胀了我的单元格布局:

R.layout.calendar_view_date_cell.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlCalendarViewCell"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white1"
 >



<TextView
    android:id="@+id/tvCalendarDayNr"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:gravity="center"
    android:padding="5dp"
    android:text="10"
    android:textSize="20sp" />

<View
    android:id="@+id/vCellBorder"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/selected_border" >
</View>

</RelativeLayout>

我想使用相对布局内的视图作为边框背景。

我正在使用此方法尝试更改单元格的背景(以显示日历的此单元格是已按下的单元格)。

 @Override
 public void onSelectDate(Date date, View view) {
      View vCellBorder = (View) view.findViewById(R.id.vCellBorder);
      vCellBorder.setBackgroundResource(R.drawable.selected_border);

      Toast.makeText(getApplicationContext(), ""+(date),
                    Toast.LENGTH_SHORT).show();


        }

奇怪的是,在我的 date_cell 预览中,我已经可以看到选定的边框。但是当我运行我的应用程序时,我只看到相对布局的背景(颜色/白色 1)。

如果我将方法更改为:

 @Override
 public void onSelectDate(Date date, View view) {

      view.setBackgroundResource(R.drawable.selected_border);

       Toast.makeText(getApplicationContext(), ""+(date),
                    Toast.LENGTH_SHORT).show();


        }

这在这里有效。但我不希望这样,因为我希望能够为布局的不同层更改不同的背景。

4

2 回答 2

1

我怀疑这是因为基类视图不知道如何正确布局。

我会将 View 更改为 ImageView 并将其放在布局中的 textview 之前,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rlCalendarViewCell"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white1"
    >
<ImageView
    android:id="@+id/vCellBorder"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/selected_border" />

<TextView
    android:id="@+id/tvCalendarDayNr"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:gravity="center"
    android:padding="5dp"
    android:text="10"
    android:textSize="20sp" />

</RelativeLayout>

同样在代码中,我将隐藏/取消隐藏视图而不是更改其可绘制对象

所以

@Override
public void onSelectDate(Date date, View view) {
  View vCellBorder = (View) view.findViewById(R.id.vCellBorder);
  vCellBorder.setVisibility(View.VISIBLE);

  Toast.makeText(getApplicationContext(), ""+(date),
                Toast.LENGTH_SHORT).show();
}

显然视图会以 GONE 或 INVISIBLE 开始

于 2013-10-17T13:35:50.957 回答
0

我的观点是 0dp 宽度和高度。所以这就是背景没有显示的原因。最后,我将视图更改为线性布局并将文本视图放入其中。match_parent 现在正在工作。0dp 给了我提示。

于 2013-10-17T13:11:10.473 回答