动态地在LinearLayout上添加TextView

时间:2014-01-29 16:36:32

标签: android android-layout

我有这个布局:

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/rect"
            android:id="@+id/searchRelativeLayout">
        <LinearLayout
                android:id="@+id/linearLayoutProc"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:weightSum="1"
                android:layout_alignParentRight="false"
                android:gravity="center"
                android:layout_marginLeft="8dp"
                android:layout_marginTop="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginBottom="2dp">
            <EditText
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:id="@+id/inputQuery"
                    android:inputType="text"
                    android:ellipsize="middle"
                    android:hint="@string/search_hint"
                    style="@style/AutoCompleteTextViewOrangeAutoComplete"/>
        </LinearLayout>
</RelativeLayout>

我正在尝试添加TextView动态地LinearLayout,但没有成功,最后,LinearLayout应该低于添加的TextView RelativeLayout compareLayout = (RelativeLayout) layoutToShow.findViewById(R.id.searchRelativeLayout); TextView compareItemOneMessage = new TextView(mContext); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); compareItemOneMessage.setLayoutParams(lp); compareItemOneMessage.setTextAppearance(mContext, android.R.style.TextAppearance_Large); compareItemOneMessage.setGravity(Gravity.CENTER); compareItemOneMessage.setTypeface(fontBold); compareItemOneMessage.setTextColor(mContext.getResources().getColor(R.color.orange)); compareItemOneMessage.setText("test"); compareLayout.addView(compareItemOneMessage); 1}}

这是我的代码:

TextView

添加了新的EditText,但它与{{1}}

的内容重叠

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

您需要添加规则以使其保持在最前面

compareItemOneMessage.addRule(RelativeLayout.ALIGN_PARENT_TOP);

并且您还需要为linearlayout添加规则以将其放下。

LinearLayout l =(LinearLayout) findViewById(R.id.linearLayoutProc);
RelativeLayout.LayoutParams params = l.getLayoutParams();
l.addRule(RelativeLayout.BELOW, your_text_view_id);

答案 1 :(得分:0)

我会做那样的事情:

// Create the textView here


compareLayout.addView(compareItemOneMessage);
List<View> views = new ArrayList<View>();
views.add(compareItemOneMessage);
for(int i=0; i<compareLayout.getChildCount(); ++i)
{
  views.add( compareLayout.getChildAt(i) );
  compareLayout.removeView( compareLayout.getChildAt(i) );
}

for (int i=0; i<views.size(); i++) compareLayout.addView(views.get(i));

这应该可以胜任。我没有测试它,因为我不在开发桌面上。

ViewGroup parent = (ViewGroup) view.getParent();
LayoutParams layoutParams = view.getLayoutParams();
parent.addViewInLayout(compareItemOneMessage, 0, layoutParams);

答案 2 :(得分:0)

我无法想象它的外观和你得到的重叠(也许你应该提供一个图像)。 但为什么你用RelativeLayout而不是LinearLayout作为root? LinearLayout在大多数情况下效果更好。

您可以尝试paddingmargin来修复重叠。

也可能是因为orientation

Layout

IllegalStateException的问题可以通过这种方式修复(但如果你知道你的观点的生命周期是什么,那么只能以这种方式重用东西)

ViewGroup vg = (ViewGroup) view.getParent();
vg.removeView(view);
//then do the stuff that adds the view - whatever failed before
linearLayout.add(view);