2015-09-23 122 views
0

我有两个片段:第一个是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(); 
    } 

} 
+0

你真的需要两个单独的片段吗?我认为将这两种布局放在一个碎片内会很简单。 – scottyseus

+0

尝试match_parent的列表视图 –

+0

@JawadLeWywadi在运行时导致错误,然后启动(?idk如何),但我看不到editText – H3LPPL0X

回答

0

您应该删除,重ATTRIB这两个片段都是。添加新的待办事项时,重量会导致InsertFragment部分推送到屏幕的可见部分之外。

+0

它只是显示一个白色的屏幕 – H3LPPL0X

+0

@ H3LPPL0X:我似乎已经与'layout_height'和-'width'混淆了,对不起。所有'layout_height'都设置为'wrap_content',这是正确的。权重增加了特殊的行为,但它们不会导致屏幕变空,所以问题必须在其他地方。 使用你的实现,我得到一个'InsertFragment'包含'EditText'和一个空的空白'ListFragment'的屏幕。当添加新的Todos时(参见http://developer.android.com/training/basics/fragments/communicating.html),它们在“ListFragment”中可见。您的EditText是否可见? – Wusii

+0

@ H3LPPL0X:忽略我对'layout_height'所说的一切。您的确切代码适用于我:两个片段都是可见的,尽管最初“ListFragment”是空白的。 – Wusii

0

android:layout_width="match_parent"放在你的片段标签中而不是android:layout_height="wrap_content"。因为layout_weight不能这样工作。

Here是一个很好的教程。

相关问题