【问题标题】:Problems with setBackgroundResourcesetBackgroundResource 的问题
【发布时间】:2012-06-10 07:10:11
【问题描述】:

我在使用 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:background="@drawable/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 作为发送。 这告诉它在屏幕中插入此布局并在该特定视图周围放置边框。

【问题讨论】:

    标签: android android-view setbackground


    【解决方案1】:

    闪电战,

    您声明只有在您尝试在代码中完成整个事情之后它才不起作用。这意味着您更改了代码以增强功能。在仔细检查了你的代码到最精细的细节之后,我相信这里的错误是你将 View 膨胀了两次,添加了一次,并更改了未添加的 View 的背景。这可能发生在进行更改时。我来解释一下:

    在块的开头:

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

    稍后,您添加新视图:

    contentScreenRoot.addView(contentView, lp);
    

    你还没有做任何背景的东西,那么我们进入下一部分。

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

    这与 contentView 具有相同的 ID,因此我们知道您正在膨胀相同的布局。但是,从来没有执行过addView,所以它只是坐在那里,稍后会消失。显然,您不能让父级和视图相同,因此您从父级获得了实际视图。

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

    就是这样,除了有一个问题。首先,contentView 已经定义好了。其次,它在未添加的组中查找视图。它会更改新 contentView 的背景,但不会做任何其他事情,因为它永远不会显示。

    建议的解决方案:

    将您的代码更改为以下内容(cmets 解释更改)

    // --> 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);
    

    希望这会有所帮助,

    模糊逻辑

    【讨论】:

    • 没有帮助。我在2个地方试过。在代码的末尾和“if”内部,两者都不起作用。 :(
    • 您实际上是在哪里执行对背景的更改? (你不包括封装函数)
    • 没有回答问题。这就是那里的所有代码。在此之前有代码将此视图置于预先准备好的布局的更高级别之一中。我已经添加了上面的代码。
    • 我的意思是,你在哪个函数中调用这段代码?在 onCreate() 中?在 onDraw() 中?最终,我需要知道这是在 LifeCycle 或 Draw Pass 中的哪个位置发生的。
    • 这是一个非常巧妙的控制外观和感觉的想法。我正在处理一个巨大的帖子,所以我将在大约 30 分钟内重新访问它。抱歉让您久等了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-18
    相关资源
    最近更新 更多