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預覽我已經可以看到所選的邊界。但是當我運行我的應用程序時,我只能看到Relativelayout(color/white1)的背景。

如果我改變我的方法:

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

     view.setBackgroundResource(R.drawable.selected_border); 

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


     } 

這在這裏工作。但我不想這樣,因爲我希望能夠爲佈局的不同層改變不同的背景。

回答

0

我的看法是獲得0dp的寬度和高度。所以這就是爲什麼沒有顯示背景。 最後,我將視圖更改爲線性佈局,並將其放入其中。並且match_parent現在正在工作。 0dp給了我提示。

1

我懷疑這是因爲基類視圖不知道如何正確佈局。

我會查看更改爲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或不可見