2014-10-29 160 views
0

我想删除添加的文本框,并添加其他基于从微调框中选择的内容。 这里是我的代码: 在XML如何刷新布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:background="#f4f4f4" 
android:id="@+id/taskoptionfragmentlinearlayout" > 


<TextView 
     android:layout_marginTop="10dp" 
     android:textStyle="bold" 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Select A Search Option" /> 
    <Spinner 
     android:layout_marginTop="20dp" 
     android:id="@+id/gis_search" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 
    <ProgressBar 
     android:id="@+id/progressBar1" 
     style="?android:attr/progressBarStyleLarge" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     /> 
    </LinearLayout> 

类:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)   
    { 
    final View rootView = inflater.inflate(R.layout.fragment_tasklist_options, container, false); 

     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, 
       int position, long id) { 
      ll = (LinearLayout)rootView.findViewById(R.id.taskoptionfragmentlinearlayout); 
      //create textfields.... 
      TextView tv = new TextView(getActivity()); 
      tv.setText(INPUTFLDLABELAArray.get(i)); 
      tv.setGravity(Gravity.RIGHT); 
      tv.setTextSize(18); 
      tv.setTypeface(Typeface.DEFAULT_BOLD); 
      tv.setPadding(0, 0, 15, 0); 
      ll.addView(tv); 
     } 

我总结了代码,使其短。 因此,当选择微调器中的项目时,显示文本字段。那么当选择另一个项目时,将在前面的文本框下显示一个文本框。所以我想要的是(刷新/重置)布局,以便每次选择某个项目时,旧的文本字段将被删除并添加新的项目。

+0

你需要的可能只是'll.invalidate()':http://developer.android.com/reference/android/view/View.html#invalidate () – Blacklight 2014-10-29 11:03:46

+0

我试过了(在ll的声明下),但没有任何改变 – 2014-10-29 11:06:13

+0

在声明之下还为时过早,在添加'TextView'之后,它必须是最后的调用。另外,如果你想替换视图,你需要在添加之前删除旧视图:http://developer.android.com/reference/android/view/ViewGroup.html#removeAllViews() – Blacklight 2014-10-29 11:07:40

回答

0

尝试添加新的之前调用ll.removeAllViews()

+0

这个工程,但这也会删除微调......不仅是以编程方式添加的内容。 – 2014-10-29 11:12:30

+0

我在调用ll.removeAllViews()后添加了微调器和其他所有东西。谢谢 – 2014-10-29 11:30:28

+0

这不是很干净。使用另一个'LinearLayout'仅用于'TextView',而不用没有包装的TextView。然后仅在该容器布局上操作。 – Blacklight 2014-10-29 11:37:22