【问题标题】:handling programatically created layout from xml: ids challenge以编程方式处理从 xml 创建布局:ids 挑战
【发布时间】:2017-09-20 17:34:50
【问题描述】:

我需要能够以编程方式多次从 xml 文件向我的主布局添加布局。

问题在于处理分配新布局视图的 ID。见代码:

MainFragment.java

private int belowOfWhat = R.id.layout_header;
...

 onActivityResult:

LayoutInflater myInflater = LayoutInflater.from(getContext());
RelativeLayout layout = (RelativeLayout) myInflater.inflate(R.layout.assignment_layout, null, false);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
//here problems start
//i want to position the new layout below the previously created layout
params.addRule(RelativeLayout.BELOW, belowOfWhat);
layout.setLayoutParams(params);
mRelativeLayout = rootView.findViewById(R.id.to_do_layout);
mRelativeLayout.addView(layout);
belowOfWhat = generateViewId();
idsOfToDos.add(belowOfWhat);
CheckBox assignmentCheckbox = (CheckBox) 
//assignment_checkbox is an id of checkbox in the xml layout I add
rootView.findViewById(R.id.assignment_checkbox);
assignmentCheckbox.setId(belowOfWhat);
assignmentCheckbox.setText(mToDoInfo);

我不知道问题出在哪里,下面是应用程序现在的工作方式:我添加了一个新布局,它正确定位在 layout_header 下方

但是当我添加第二个或更多布局时,它们在应用程序顶部相互重叠,而不是一个位于另一个下方。

如果您能指导我解决问题,我将不胜感激。

【问题讨论】:

    标签: android xml android-layout android-fragments onactivityresult


    【解决方案1】:

    如果您不需要视图的 ID 用于其他目的,您可以更简单地解决。

    如果我理解你想要做什么,你需要的是一个垂直的LinearyLayout而不是RelativeLayout,然后子视图将被添加到彼此下方

    【讨论】:

    • 这就是我会做的。但是应用程序的右下角有一个浮动按钮。是否可以在线性布局中将浮动按钮定位在右下角?
    • 尝试有一个相对的布局来放置一个浮动按钮和一个线性布局。因此,您可以将浮动按钮放置在您想要的位置,然后您可以将线性布局放置在相对布局的顶部,然后以编程方式添加视图而不会出现问题
    【解决方案2】:

    您必须使用相对布局并使用以下属性来对齐视图。

    • RelativeLayout.BELOW
    • RelativeLayout.RIGHT_OF
    • RelativeLayout.ALIGN_BOTTOM

    RelativeLayout 布局 = new RelativeLayout(this); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); layout.setLayoutParams(layoutParams);

    RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    
    TextView tv1 = new TextView(this);
    tv1.setId(1);
    tv1.setText("textView1");
    
    TextView tv2 = new TextView(this);
    params2.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
    tv2.setId(2);
    tv2.setText("textView2");
    
    TextView tv3 = new TextView(this);
    params3.addRule(RelativeLayout.BELOW, tv1.getId());
    tv3.setId(3);
    tv3.setText("textView3");
    
    TextView tv4 = new TextView(this);
    params4.addRule(RelativeLayout.RIGHT_OF, tv3.getId());
    params4.addRule(RelativeLayout.ALIGN_BOTTOM, tv3.getId());
    tv4.setId(4);
    tv4.setText("textView4");
    
    layout.addView(tv1, params1);
    layout.addView(tv2, params2);
    layout.addView(tv3, params3);
    layout.addView(tv4, params4);
    

    希望它能让您了解如何务实地对齐视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-01
      • 2019-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多