我有两个片段:第一个是editText,其中suer可以键入文本,第二个是他们键入的内容的列表。Android无法将两个片段显示在屏幕上
我试图让这两个片段出现,但不能为我的生活得到他们。如果我不想同时使用bot,我可以通过它自己显示编辑文本。
如果任何人都可以提供帮助,我将不胜感激,我在想我在某个地方有一些愚蠢的错误。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="---.ListFragment"
android:orientation="vertical">
<ListView
android:id="@+id/myListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="---.InsertFragment"
android:orientation="vertical">
<EditText
android:id="@+id/myEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/addItemHint" />
</LinearLayout >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".TodoListActivity">
<fragment
android:id="@+id/insertFragment"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
class="---.InsertFragment" >
</fragment>
<fragment
android:id="@+id/listFragment"
android:layout_width="match_parent"
android:layout_weight="2"
android:layout_height="wrap_content"
class="---.ListFragment" >
</fragment>
</LinearLayout>
public class TodoListActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_todo_list);
}
}
public class ListFragment extends Fragment {
ArrayList<String> todoItems = null;
ArrayAdapter<String> aa = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list, container, false);
todoItems = new ArrayList<String>();
aa = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1, todoItems);
ListView myListView = (ListView) view.findViewById(R.id.myListView);
myListView.setAdapter(aa);
return view;
}
public void addItem(String item) {
todoItems.add(0, item);
aa.notifyDataSetChanged();
}
}
你真的需要两个单独的片段吗?我认为将这两种布局放在一个碎片内会很简单。 – scottyseus
尝试match_parent的列表视图 –
@JawadLeWywadi在运行时导致错误,然后启动(?idk如何),但我看不到editText – H3LPPL0X