[英]Linear Layout doesn't appear on phone although it appears on the design page
每当我进行线性布局时,整个布局都会完美地显示在设计页面上,但是当我在手机上运行该应用程序时,该布局将显示为完全空白。
这是未出现的activity_order_list.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/txtInput"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:hint="New item"
/>
<Button
android:id="@+id/btAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:layout_toRightOf="@+id/txtInput"
/>
</RelativeLayout>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
这是item_list.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txtview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
这是它的Java代码:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
public class OrderList extends Activity {
ArrayAdapter<String> adapter;
EditText editText;
ArrayList<String> itemList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
String[] items={"Apple","Banana","Clementine"};
itemList=new ArrayList<String>(Arrays.asList(items));
adapter=new ArrayAdapter<String> (this,R.layout.list_item,R.id.txtview,itemList);
ListView mainlist=(ListView)findViewById(R.id.list);
mainlist.setAdapter(adapter);
editText=(EditText)findViewById(R.id.txtInput);
Button btAdd=(Button)findViewById(R.id.btAdd);
btAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String newItem=editText.getText().toString();
// add new item to arraylist
itemList.add(newItem);
// notify listview of data changed
adapter.notifyDataSetChanged();
}
});
}
}
将fill_parent更改为match_parent
尝试更改setContentView(R.layout.list_item);
设置setContentView(R.layout.activity_order_list);
另外,如果要填充LinearLayout
所有空间,还必须设置ListView
的layout_weight
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>
首先,您输入item_list.xml
作为您在ListView中使用的布局的名称,并在Activity中输入了list_item
因此请首先确保名称正确。 其次,您将setContentView
的contentview设置为R.layout.list_item
,这是ListView的布局,其中仅包含TextView而没有任何文本。 因此,当您的应用程序运行时,它不会向您显示activity_order_list.xml
的布局,而是将显示list_item.xml
的布局,其中仅包含一个TextView而没有任何文本。 这将使屏幕看起来空白。 因此,首先更改您的setContentView(R.layout.list_item);
设置setContentView(R.layout.activity_order_list);
还可以将layout_height
从match_parent
更改为item_list.xml
wrap_content
,然后让我们知道这是否解决了您的问题...希望对您有所帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.