2012-06-10 34 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

相关问题