sparrowHuhuhu
2017-05-10 13:32
采纳率: 100%
浏览 1.5k

layout中listview占据了整个界面,其余控件全部隐藏,xml文件怎么改都没用

layout中listview占据了整个界面,其余控件全部隐藏,xml文件怎么改都没用,只要listview有数据,就看不到添加的BUTTON或者EXITVIEW了。

布局文件:

<?xml version="1.0" encoding="utf-8"?>

xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<Button
    android:id="@+id/btshanchu"
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"
    android:text="删除" 
   />


<ListView  
    android:id="@+id/Lziliao"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:layout_below="@id/btshanchu"/>

java文件:

package com.example.huclass;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;

public class GuanliActivity extends Activity {

 private ListView listView; 
              @Override  
    public void onCreate(Bundle savedInstanceState){  
        super.onCreate(savedInstanceState);  

        listView = new ListView(this);         
        //android.R.layout.simple_list_item_1是系统定义好的布局文件只显示一行文字  
        listView.setAdapter((ListAdapter) new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getData()));  
        setContentView(listView);  

        listView.setAdapter(new ArrayAdapter<String>(this,  
                android.R.layout.simple_list_item_multiple_choice, getData()));  

        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);}

private List getData(){

        List<String> data = new ArrayList<String>();  
        data.add("【计算机网络】数据链路层");  
        data.add("【计算机网络】应用层");  
        data.add("【信息论】香农定理");  
        data.add("【概率论】总结");  

        return data;  
    }  

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.guanli, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • ziyejinwei1994 2017-05-11 01:24
    已采纳

    setContentView(listView);

    里面那个listview改成你xml的名字 R.layout.XXX

    点赞 打赏 评论
  • 志尊宝 2017-05-10 13:52

    用RelativeLayout布局 你是想把button放在什么位置就都可以显示了

    点赞 打赏 评论
  • YXTS122 2017-05-10 13:53

    setContentView(listView);你都这样写了,肯定只有listview了,另外怎么两个listView.setAdapter?????

    点赞 打赏 评论
  • sparrowHuhuhu 2017-05-11 06:45

    public class ZhuyeActivity extends ListActivity {

    //通过一个list去映射数据显示在listview中

    private List> mData;

    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        mData = getData();  
        MyAdapter adapter = new MyAdapter(this);  
        setListAdapter(adapter); 
    }  
    
    private List<Map<String, Object>> getData() {  
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();  
    
        Map<String, Object> map = new HashMap<String, Object>();  
        map.put("title", "1");  
        map.put("info", " 1 ");  
        map.put("img", R.drawable.ic_launcher);  
        list.add(map);  
    
        map = new HashMap<String, Object>();  
        map.put("title", "2");  
        map.put("info", " 2 ");  
        map.put("img", R.drawable.ic_launcher);  
        list.add(map);  
    
        map = new HashMap<String, Object>();  
        map.put("title", "3");  
        map.put("info", " 3 ");  
        map.put("img", R.drawable.ic_launcher);  
        list.add(map);  
    
        return list;  
    }  
    
    // ListView 中某项被选中后的逻辑  
    @Override  
    protected void onListItemClick(ListView l, View v, int position, long id) {  
    
        Log.v("MyListView4-click", (String) mData.get(position).get("title"));  
    }  
    
    /** 
     * listview中点击按键弹出对话框 
    
    public void showInfo() {  
        new AlertDialog.Builder(this).setTitle("Btn listview")  
                .setMessage("123")  
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {  
                    public void onClick(DialogInterface dialog, int which) {  
                    }  
                }).show();  
    
    }  */ 
    
    public final class ViewHolder {  
        public ImageView img;  
        public TextView title;  
        public TextView info;  
        public Button viewBtn;  
    }  
    
    public class MyAdapter extends BaseAdapter {  
    
        private LayoutInflater mInflater;  
    
        public MyAdapter(Context context) {  
            this.mInflater = LayoutInflater.from(context);  
        }  
    
        public int getCount() {  
            // TODO Auto-generated method stub  
            return mData.size();  
        }  
    
        public Object getItem(int arg0) {  
            // TODO Auto-generated method stub  
            return null;  
        }  
    
        public long getItemId(int arg0) {  
            // TODO Auto-generated method stub  
            return 0;  
        }  
    
        public View getView(int position, View convertView, ViewGroup parent) {  
    
            ViewHolder holder = null;  
            if (convertView == null) {  
    
                holder = new ViewHolder();  
    
                convertView = mInflater.inflate(R.layout.list, null);  
                holder.img = (ImageView) convertView.findViewById(R.id.img);  
                holder.title = (TextView) convertView.findViewById(R.id.title);  
                holder.info = (TextView) convertView.findViewById(R.id.info);  
                holder.viewBtn = (Button) convertView  
                        .findViewById(R.id.view_btn);  
                convertView.setTag(holder);  
    
            } else {  
    
                holder = (ViewHolder) convertView.getTag();  
            }  
    
            holder.img.setBackgroundResource((Integer) mData.get(position).get(  
                    "img"));  
            holder.title.setText((String) mData.get(position).get("title"));  
            holder.info.setText((String) mData.get(position).get("info"));  
    
            holder.viewBtn.setOnClickListener(new View.OnClickListener() {  
    
                public void onClick(View v) {  
                     Intent intent = new Intent();
                     intent.setClass(ZhuyeActivity.this, KechengActivity.class);
                     ZhuyeActivity.this.startActivity(intent);  
                }  
            });  
    
            return convertView;  
        }  
    
    }  public class ZhuyeActivity extends ListActivity {  
    //通过一个list去映射数据显示在listview中  
    private List<Map<String, Object>> mData;  
    
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        mData = getData();  
        MyAdapter adapter = new MyAdapter(this);  
        setListAdapter(adapter); 
    }  
    
    private List<Map<String, Object>> getData() {  
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();  
    
        Map<String, Object> map = new HashMap<String, Object>();  
        map.put("title", "1");  
        map.put("info", " 1 ");  
        map.put("img", R.drawable.ic_launcher);  
        list.add(map);  
    
        map = new HashMap<String, Object>();  
        map.put("title", "2");  
        map.put("info", " 2 ");  
        map.put("img", R.drawable.ic_launcher);  
        list.add(map);  
    
        map = new HashMap<String, Object>();  
        map.put("title", "3");  
        map.put("info", " 3 ");  
        map.put("img", R.drawable.ic_launcher);  
        list.add(map);  
    
        return list;  
    }  
    
    // ListView 中某项被选中后的逻辑  
    @Override  
    protected void onListItemClick(ListView l, View v, int position, long id) {  
    
        Log.v("MyListView4-click", (String) mData.get(position).get("title"));  
    }  
    
    /** 
     * listview中点击按键弹出对话框 
    
    public void showInfo() {  
        new AlertDialog.Builder(this).setTitle("Btn listview")  
                .setMessage("123")  
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {  
                    public void onClick(DialogInterface dialog, int which) {  
                    }  
                }).show();  
    
    }  */ 
    
    public final class ViewHolder {  
        public ImageView img;  
        public TextView title;  
        public TextView info;  
        public Button viewBtn;  
    }  
    
    public class MyAdapter extends BaseAdapter {  
    
        private LayoutInflater mInflater;  
    
        public MyAdapter(Context context) {  
            this.mInflater = LayoutInflater.from(context);  
        }  
    
        public int getCount() {  
            // TODO Auto-generated method stub  
            return mData.size();  
        }  
    
        public Object getItem(int arg0) {  
            // TODO Auto-generated method stub  
            return null;  
        }  
    
        public long getItemId(int arg0) {  
            // TODO Auto-generated method stub  
            return 0;  
        }  
    
        public View getView(int position, View convertView, ViewGroup parent) {  
    
            ViewHolder holder = null;  
            if (convertView == null) {  
    
                holder = new ViewHolder();  
    
                convertView = mInflater.inflate(R.layout.list, null);  
                holder.img = (ImageView) convertView.findViewById(R.id.img);  
                holder.title = (TextView) convertView.findViewById(R.id.title);  
                holder.info = (TextView) convertView.findViewById(R.id.info);  
                holder.viewBtn = (Button) convertView  
                        .findViewById(R.id.view_btn);  
                convertView.setTag(holder);  
    
            } else {  
    
                holder = (ViewHolder) convertView.getTag();  
            }  
    
            holder.img.setBackgroundResource((Integer) mData.get(position).get(  
                    "img"));  
            holder.title.setText((String) mData.get(position).get("title"));  
            holder.info.setText((String) mData.get(position).get("info"));  
    
            holder.viewBtn.setOnClickListener(new View.OnClickListener() {  
    
                public void onClick(View v) {  
                     Intent intent = new Intent();
                     intent.setClass(ZhuyeActivity.this, KechengActivity.class);
                     ZhuyeActivity.this.startActivity(intent);  
                }  
            });  
    
            return convertView;  
        }  
    
    }  
    
    点赞 打赏 评论
  • yangshuaiMAX 2017-05-11 06:54

    根布局不要用LinearLayout

    点赞 打赏 评论
  • 接口的说法 2017-05-11 08:16

    @Override

    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.xxxx)
    mData = getData();

    MyAdapter adapter = new MyAdapter(this);

    setListAdapter(adapter);
    }

    点赞 打赏 评论

相关推荐 更多相似问题