2012-06-10 31 views
2

問題我有setBackgroundResource奇怪的問題。 出於某種原因,當我運行folling代碼,它不設置backgropund。與setBackgroundResource

layoutId的是,我想添加到另一個佈局的ID。 這是在此之前執行,然後我運行此代碼。

viewToBorderId是佈局內的視圖的ID。圍繞這一觀點,我希望放置邊界。

ViewGroup contentView = (ViewGroup) this.getLayoutInflater().inflate(layoutId, null); 
    RelativeLayout contentScreenRoot = (RelativeLayout) this.findViewById(R.id.content_screen_root); 
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
     ((ScrollView) contentScreenRoot.findViewById(R.id.content_screen_scroll_view)).getLayoutParams()); 
    lp.addRule(RelativeLayout.BELOW, R.id.content_screen_user_details); 
    lp.setMargins(2, 0, 2, 0); 

    contentScreenRoot.addView(contentView, lp); 
    ViewGroup contentViewParent = (ViewGroup) this.getLayoutInflater().inflate(layoutId, null); 
    ViewGroup contentView = (ViewGroup) contentViewParent.findViewById(viewToBorderId); 

    GradientDrawable gd ; 
    gd = (GradientDrawable) contentView.getBackground(); 
    if (gd == null){ 
     contentView.setBackgroundResource(R.drawable.content_screen_borders_no_rounding); 
     gd = (GradientDrawable) contentView.getBackground(); 
    } 

    gd.setCornerRadii(new float[]{0,0,0,0,0,0,20-3,20-3}); 

    contentView.setBackgroundDrawable(gd); 

R.drawable.content_screen_borders_no_rounding

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    > 
    <stroke 
     android:width="@dimen/content_screen_scroll_view_border" 
     android:color="@color/content_screen_scroll_view_border" 
     /> 
    <padding 
     android:left="0dip" 
     android:top="@dimen/content_screen_scroll_view_border"    
     android:right="0dip" 
     android:bottom="@dimen/content_screen_scroll_view_border" 
     /> 
    <gradient 
     android:angle="270" 
     android:startColor="@color/listview_selected_item_start" 
     android:endColor="@color/listview_selected_item_end" 
     android:type="linear" 
     /> 
</shape> 

奇怪的是,如果我採用了android設置背景事先:後臺= 「@繪製/ content_screen_borders_no_rounding」 然後它工作正常。邊界在那裏,當我做最後一點的代碼繞角的時候它就起作用了。 只有當我從代碼中完成整件事情時纔會失敗。

「基地」 佈局其他佈局被插入到其中:

<?xml version="1.0" encoding="UTF-8"?> 

<RelativeLayout 
    android:id="@+id/content_screen_secondary_title" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="5dip" 
    > 

    <ImageView 
     android:id="@+id/content_screen_icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingLeft="20dip" 
     android:paddingRight="20dip" 
     android:layout_alignParentLeft="true" 
     android:gravity="top|left" 
     /> 

    <!-- The following RelativeLayout container is required to force the Hebrew text on some devices to appear at the right of the background image instead of the left on certain devices --> 
    <RelativeLayout 
     android:id="@+id/content_screen_helper" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/content_screen_icon" 
     android:background="@drawable/title_bg" 
     android:layout_alignParentRight="true" 
     android:gravity="center_vertical" 
     android:layout_centerVertical="true" 
     > 

     <TextView 
      android:id="@+id/content_screen_title_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="@color/content_text_color" 
      android:textSize="18sp" 
      android:textStyle="bold" 
      android:gravity="center_vertical|right" 
      android:layout_alignParentRight="true" 
      android:paddingRight="25dip" 
      /> 

    </RelativeLayout> 

</RelativeLayout> 

<RelativeLayout 
    android:id="@+id/content_screen_user_details" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/user_details_bg" 
    android:layout_below="@id/content_screen_secondary_title" 
    android:layout_marginBottom="4dip" 
    android:layout_marginLeft="20dip" 
    android:visibility="gone" 
    > 
    <!-- value for visibility can be either 'visible' or 'gone' --> 

    <TextView 
     android:id="@+id/content_screen_user_name_prompt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:paddingRight="25dip" 
     android:text="@string/content_screen_name_prompt" 
     android:textSize="12sp" 
     android:textColor="@color/content_text_color" 
     android:layout_centerVertical="true" 
     android:gravity="center_vertical" 
     /> 
    <TextView 
     android:id="@+id/content_screen_user_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toLeftOf="@id/content_screen_user_name_prompt" 
     android:paddingRight="5dip" 
     android:textSize="12sp" 
     android:textColor="@color/content_text_color" 
     android:layout_centerVertical="true" 
     android:gravity="center_vertical" 
     /> 
    <TextView 
     android:id="@+id/content_screen_user_id_prompt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/content_screen_user_id" 
     android:layout_centerVertical="true" 
     android:paddingLeft="5dip" 
     android:text="@string/content_screen_id_prompt" 
     android:textSize="12sp" 
     android:textColor="@color/content_text_color" 
     /> 
    <TextView 
     android:id="@+id/content_screen_user_id" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:paddingLeft="20dip" 
     android:textSize="12sp" 
     android:textColor="@color/content_text_color" 
     /> 

</RelativeLayout> 

<ScrollView 
    android:id="@+id/content_screen_scroll_view" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_below="@id/content_screen_user_details" 
    android:background="@drawable/content_screen_borders" 
    android:layout_marginRight="10dip" 
    android:layout_marginLeft="10dip" 
    android:layout_marginBottom="10dip" 
    android:layout_marginTop="@dimen/content_screen_scroll_view_margintop" 
    android:visibility="gone" 
    > 
    <!-- Set visibility to gone, until the user calls the method: setMainContent(), which causes this ScrollView to become visible and also assigns it with its content --> 

</ScrollView> 

<!-- The following RelativeLayyout container is required to make the Hebrew text on some devices to start at the right of the background image instead of the left --> 
<RelativeLayout 
    android:id="@+id/content_screen_details_container" 
    android:layout_width="150dip" 
    android:layout_height="wrap_content" 
    android:layout_marginRight="10dip" 
    android:layout_above="@id/content_screen_scroll_view" 
    android:background="@drawable/details_bg" 
    android:gravity="center_vertical|right" 
    android:layout_alignParentRight="true" 
    android:visibility="invisible" 
    > 
    <!-- Value for visibility can be 'visible' or 'invisible' --> 

    <TextView 
     android:id="@+id/content_screen_details" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@color/content_text_color" 
     android:layout_centerVertical="true" 
     android:gravity="center_vertical|right" 
     android:paddingRight="10dip" 
     /> 

</RelativeLayout> 

然後我調用上述代碼,以添加的東西。

這是諸如此類的事情,我想補充:

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

    <ListView 
     android:id="@+id/searchable_list_list_view" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingLeft="2dip" 
     android:paddingRight="2dip" 
     android:dividerHeight="1dip" 
     android:footerDividersEnabled="true" 
     android:listSelector="@drawable/listview_selected_item_background" 
     android:fadingEdge="none" 
     android:cacheColorHint = "#00000000" 

     > 

    </ListView> 


</LinearLayout> 

我會叫上面的方法傳遞佈局的第一個ID和「searchable_list_list_view的id 「作爲發送。 這告訴它將此佈局插入屏幕中並在該特定視圖周圍放置邊框。

回答

1

theblitz,

幽州,只有在您嘗試做代碼整個事情,這是行不通的。這意味着你改變了代碼以增強功能。仔細檢查你的代碼,仔細端詳之後,我相信在這裏的錯誤是,你是充氣查看了兩次,加一次,並改變unadded視圖的背景。這可能發生在進行更改時。我將解釋:

在你塊的開頭:

ViewGroup contentView = (ViewGroup) this.getLayoutInflater().inflate(layoutId, null); 

只是有一點後,你添加新的視圖:

contentScreenRoot.addView(contentView, lp); 

您有沒有做過任何背景的東西還,那麼我們進入下一部分。

ViewGroup contentViewParent = (ViewGroup) this.getLayoutInflater().inflate(layoutId, null); 

此具有相同的ID爲內容查看,所以我們知道你正在膨脹相同的佈局。然而,沒有addView曾經執行過,所以它只是坐在那裏,稍後會消失。顯然,你不能讓父母和視圖相同,所以你從父母那裏得到了實際的視角。

ViewGroup contentView = (ViewGroup) contentViewParent.findViewById(viewToBorderId); 

這是它,除了有一個問題。首先,contentView已經被定義。其次,它找到一個沒有被添加的組內的視圖。它會更改新內容視圖的背景,但不會執行其他任何操作,因爲它從不顯示。

建議的解決方案(S):

更改您的代碼如下(註釋解釋的變化)

// --> Rename this variable. It IS the inflated Parent and we only need to do this once. 
    ViewGroup contentViewParent = (ViewGroup) this.getLayoutInflater().inflate(layoutId, null); 
    RelativeLayout contentScreenRoot = (RelativeLayout) this.findViewById(R.id.content_screen_root); 
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
    ((ScrollView) contentScreenRoot.findViewById(R.id.content_screen_scroll_view)).getLayoutParams()); 
    lp.addRule(RelativeLayout.BELOW, R.id.content_screen_user_details); 
    lp.setMargins(2, 0, 2, 0); 

// --> Align the addView statement to use the new name 
    contentScreenRoot.addView(contentViewParent, lp); 

// --> Delete this line completely. (see? its the same ID) 
//ViewGroup contentViewParent = (ViewGroup) this.getLayoutInflater().inflate(layoutId, null); 

// Now this should be using the added ViewGroup, rather than the unadded one. 
// AND it is not being redefined, like it was before. 
    ViewGroup contentView = (ViewGroup) contentViewParent.findViewById(viewToBorderId); 

    GradientDrawable gd ; 
    gd = (GradientDrawable) contentView.getBackground(); 
    if (gd == null){ 
     contentView.setBackgroundResource(R.drawable.content_screen_borders_no_rounding); 
     gd = (GradientDrawable) contentView.getBackground(); 
    } 

gd.setCornerRadii(new float[]{0,0,0,0,0,0,20-3,20-3}); 

contentView.setBackgroundDrawable(gd); 

希望這有助於

FuzzicalLogic

+0

沒有幫幫我。我在2個地方嘗試過。在代碼的末尾,在「if」之內,並且都不起作用。 :( – theblitz

+1

你在哪裏實際執行背景變化?(你不包括封裝函數) –

+0

沒有遵循這個問題,這是所有的代碼,在那之前有代碼將這個視圖我在上面的代碼中添加了上面的代碼 – theblitz

相關問題