2015-12-01 26 views
0

我想对齐在RelativeLayout片段中的for循环中动态生成的按钮,但按钮在相同的位置重叠,我查看了很多其他的帖子和我仍然不知道如何解决它,我知道使用linearLayout将解决问题,但按钮是错误的大小和位置,并出于某种原因,我不能将linearLayout添加到RelativeLayout的。我粘贴了2个代码示例,我需要一个解决方案。提前致谢。Android Align按钮动态地在RelativeLayout中片段

下面是我的代码(尝试1:校准在相对布局按钮):

View view = inflater.inflate(R.layout.home, container, false); 

RelativeLayout relativeLayout = new RelativeLayout(getActivity().getApplicationContext()); 
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
     RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 

List<String> test_list = Arrays.asList("sup1", "sup2", "sup3"); 

for (int i = 0; i < test_list.size(); i++) { 
    final String test_name = test_list.get(i); 
    Button button = new Button(getActivity()); 
    button.setText("Test" + test_name + " " + i); 
    button.setId(i); 

    if(i > 0){ 
     lp.addRule(RelativeLayout.RIGHT_OF, (i-1)); 
    } 

    relativeLayout.addView(button, lp); 

    button.setOnClickListener(new View.OnClickListener() {@Override 
     public void onClick(View v) { 
      FragmentManager fragmentmanager = getFragmentManager(); 
      FragmentTransaction transaction = fragmentmanager.beginTransaction(); // allows attach,detach,etc 
      transaction.replace(R.id.DB2Home, newFragment, "ChooseCategory").addToBackStack(null).commit(); 
     } 
    }); 
} 

((ViewGroup) view).addView(relativeLayout); 

return view; 

(尝试2:线性布局对齐按钮,并添加到相对布局)

View view = inflater.inflate(R.layout.home, container, false); 

LinearLayout linearLayout = new LinearLayout(getActivity().getApplicationContext()); 
linearLayout.setOrientation(LinearLayout.VERTICAL); 

List<String> test_list = Arrays.asList("sup1", "sup2", "sup3"); 
for (int i = 0; i < test_list.size(); i++) { 
    final String test_name = test_list.get(i); 
    Button button = new Button(getActivity()); 
    button.setText(test_name); 
    button.setId(i); 

    linearLayout.addView(button); 

    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      FragmentManager fragmentmanager = getFragmentManager(); 
      FragmentTransaction transaction = fragmentmanager.beginTransaction(); // allows attach,detach,etc 
      transaction.replace(R.id.DB2Home, newFragment,"ChooseCategory").addToBackStack(null).commit(); 
     } 
    }); 
} 
// Doesn't work for relative layout 
// RelativeLayout relativeLayout = (RelativeLayout) view.findViewById(R.id.homescreen); 
//relativeLayout.addView(linearLayout); 
//((ViewGroup) view).addView(relativeLayout); 

((ViewGroup) view).addView(linearLayout); 

return view; 

回答

0

你可以通过代码获得它:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams(); 
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of); 

button.setLayoutParams(params); //causes layout update 
+0

你可以更具体地在哪里添加它们请。我得到了NullPointerException:布局参数不能为null为RelativeLayout.LayoutParams params =(RelativeLayout.LayoutParams)button.getLayoutParams(); –