代码之家  ›  专栏  ›  技术社区  ›  Mokkapps

以编程方式将属性AlignParentRight设置为RelativeLayout中的按钮不起作用?

  •  17
  • Mokkapps  · 技术社区  · 13 年前

    我试图以编程方式将RelativeLayout中的属性AlignParentRight设置为按钮,但该程序因NullPointerException而崩溃。这里是我的代码:

    //add horizontal realative layout
    RelativeLayout layouth = new RelativeLayout(this);
    layouth.setLayoutParams(new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    
    //add textview for label
    TextView t = new TextView(this);
    t.setText(d.getName());
    t.setTextSize(25);
    t.setId(2);      
    t.setLayoutParams(new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    
    //add button
    Button b = new Button(this);
    b.setText(d.getName());
    RelativeLayout.LayoutParams layoutParams =(RelativeLayout.LayoutParams)b.getLayoutParams();
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
    layoutParams.addRule(RelativeLayout.LEFT_OF, 2);
    b.setLayoutParams(layoutParams);   
    
    //add textview and button to horizontal linear layout
    layouth.addView(t);
    layouth.addView(b);
    //add horizontal linear layout to vertical linear layout
    layoutv = (LinearLayout) findViewById(R.id.ProjectViewLinearID);
    layoutv.addView(layouth);
    

    我的问题在哪里?错误发生在程序行 layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

    使用解决方案更新:

    此代码适用于我:

    // Creating a new RelativeLayout
    RelativeLayout layouth = new RelativeLayout(this);
    
    // Defining the RelativeLayout layout parameters.
    // In this case I want to fill its parent
    RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);
    
    // Creating a new TextView
    TextView tv = new TextView(this);
    tv.setText(d.getName());
    tv.setTextSize(25);
    
    //add button
    Button b = new Button(this);
    b.setText(d.getName());
    
    // Defining the layout parameters of the TextView
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
             RelativeLayout.LayoutParams.WRAP_CONTENT,
             RelativeLayout.LayoutParams.WRAP_CONTENT);
             lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    
    // Setting the parameters on the TextView
    tv.setLayoutParams(lp);
    b.setLayoutParams(lp);
    
    // Adding the TextView to the RelativeLayout as a child
    layouth.addView(tv);
    layouth.addView(b);
    
    2 回复  |  直到 7 年前
        1
  •  14
  •   Mukesh Soni    13 年前

    代替

    RelativeLayout.LayoutParams layoutParams =(RelativeLayout.LayoutParams)b.getLayoutParams();
    

    具有

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
    
        2
  •  8
  •   njzk2    13 年前

    当你打电话时 b.getLayoutParams() ,b不在布局中。因此,它没有layoutparams(此外,它不知道您此时正在期待RelativeLayout.layoutparams)。

    您需要创建一个新的LayoutParams实例,就像您为TextView所做的那样。