Android 挫折资源的问题

Android 挫折资源的问题,android,android-view,setbackground,Android,Android View,Setbackground,我在挫折资源方面遇到了奇怪的问题。 由于某种原因,当我运行folling代码时,它没有设置backgropund layoutId是我希望添加到另一个布局的布局id。 这是在此之前执行的,然后我运行此代码 ViewToBurderId是该布局中视图的id。我希望围绕这一观点划定边界 ViewGroup contentView = (ViewGroup) this.getLayoutInflater().inflate(layoutId, null); RelativeLayout

我在挫折资源方面遇到了奇怪的问题。 由于某种原因,当我运行folling代码时,它没有设置backgropund

layoutId是我希望添加到另一个布局的布局id。 这是在此之前执行的,然后我运行此代码

ViewToBurderId是该布局中视图的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.可绘制内容\屏幕\边框\无四舍五入

    <?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\u screen\u borders\u no\u rounding”提前设置背景 那就行了。边界在那里,当我为圆角做最后一点代码时,它就工作了。 只有当我在代码中做了大量工作时,它才会失败

插入其他布局的“基本”布局:

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


然后我调用上面的代码来添加一些东西

我想补充一点:


我将调用上面的方法,将布局作为第一个id传递,并将“searchable\u list\u view”的id作为发送。 这会告诉它在屏幕中插入此布局,并在特定视图周围添加边框。

闪电战

您声明,只有在您尝试用代码完成整个工作之后,它才不起作用。这意味着您更改了代码以增强功能。在仔细检查代码之后,我认为这里的错误是,您将视图膨胀两次,添加一次,并更改未添加视图的背景。这可能是在进行更改时发生的。我会解释:

在区块的开始处:

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的背景,但不会执行任何其他操作,因为它从不显示

建议的解决方案:

将代码更改为以下内容(注释解释更改)

希望这有帮助


模糊逻辑

没有帮助。我在两个地方试过。在代码末尾和“如果”内,两者都不起作用:(您实际在哪里执行对背景的更改?(您不包括封装功能)我没有回答这个问题。这是所有的代码。之前有一些代码将此视图放在预先准备好的布局的更高级别中。我已经在上面添加了代码。我的意思是,您在哪个函数中调用此代码?在onCreate()中?在onDraw()中最终,我需要知道生命周期中的哪一个或是通过这个过程发生。这是一个很好的控制外观和感觉的方法。我在一个巨大的帖子的中间,所以我将在大约30分钟内重温这一点。抱歉等待。
ViewGroup contentViewParent = (ViewGroup) this.getLayoutInflater().inflate(layoutId, null);
ViewGroup contentView = (ViewGroup) contentViewParent.findViewById(viewToBorderId);
// --> 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);