編輯:關於Android編程
當我們直接在布局文件中寫三個listview的時候,會出現三個滾動條,並且每個listview都只顯示一個item,要改動才顯示更多。怎麼做才好了?
辦法是有得:用一個ScrollView包含一個大布局,然後把這三個listview加到裡面去。
新建一個布局文件:<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PD94bWwgdmVyc2lvbj0="1.0" encoding="utf-8"?>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fadingEdge="none"
android:scrollbars="vertical">
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
一個Java文件:
package com.test;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import com.listviewtest.R;
/**
* 三個Listview共享一個滾動條
* @author mark
*
*/
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉標題欄
setContentView(R.layout.main);
initView();
addFirstListview();
addSecondListview();
addThirdListview();
//scrollView.addView(all);
}
ScrollView scrollView;
LinearLayout all;
private void initView() {
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
//LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.main, null);
//layout_topic = (LinearLayout) linearLayout.findViewById(R.id.linearlayout);
all = (LinearLayout) findViewById(R.id.linearlayout);
scrollView = (ScrollView) findViewById(R.id.scrollview);
}
private void addFirstListview( ){
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
textView.setText("第一個列表");
textView.setGravity(Gravity.LEFT);
textView.setTextSize(18);
textView.setTextColor(Color.BLACK);
all.addView(textView);
ArrayList
HashMap
for (int i = 1; i <= 10; i ++) {
map = new HashMap
map.put("text", "text" + i );
arraylist.add(map);
}
ListViewAdapter listViewAdapter = new ListViewAdapter(MainActivity.this, arraylist);
ListView listView = new ListView(MainActivity.this);
int height = arraylist.size() * (int) getResources().getDimension(R.dimen.listview_item_height);
listView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, height));
listView.setDividerHeight(1);
listView.setAdapter(listViewAdapter);
all.addView(listView);
}
private void addSecondListview( ){
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
textView.setText("第二個列表");
textView.setGravity(Gravity.LEFT);
textView.setTextSize(18);
textView.setTextColor(Color.BLACK);
all.addView(textView);
ArrayList
HashMap
for (int i = 11; i <= 20; i ++) {
map = new HashMap
map.put("text", "text" + i );
arraylist.add(map);
}
ListViewAdapter listViewAdapter = new ListViewAdapter(MainActivity.this, arraylist);
ListView listView = new ListView(MainActivity.this);
int height = arraylist.size() * (int) getResources().getDimension(R.dimen.listview_item_height);
listView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, height));
listView.setDividerHeight(1);
listView.setAdapter(listViewAdapter);
all.addView(listView);
}
private void addThirdListview( ){
TextView textView = new TextView(MainActivity.this);
textView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
textView.setText("第三個列表");
textView.setGravity(Gravity.LEFT);
textView.setTextSize(18);
textView.setTextColor(Color.BLACK);
all.addView(textView);
ArrayList
HashMap
for (int i = 21; i <= 30; i ++) {
map = new HashMap
map.put("text", "text" + i );
arraylist.add(map);
}
ListViewAdapter listViewAdapter = new ListViewAdapter(MainActivity.this, arraylist);
ListView listView = new ListView(MainActivity.this);
int height = arraylist.size() * (int) getResources().getDimension(R.dimen.listview_item_height);
listView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, height));
listView.setDividerHeight(1);
listView.setAdapter(listViewAdapter);
all.addView(listView);
}
}
其中設置listview的高度是重點。
項目源代碼在這裡,需要可以下載
續: 話說道哥跟發哥在上海柯炫做Android開發,看來跟發哥差距很大。總結了下發哥的技術拿來這裡裝逼用用。 參考這裡: http://blog.csd
自定義View通訊錄字母快速索引在Android日常開發中,我們經常在聯系人界面看到一些字母導航欄,點擊字母的時候,會根據漢字的首拼音來查找是否存在相應的item,這種效
1.應用場景ProgressBar主要的應用場景在於對網絡請求,數據加載顯示時由於需要用戶等待,如果沒有提示有可能造成用戶退出,或者誤認為程序錯誤,增加ProgressB
Statement:This archive is created by William Yi and it’s all the harvests which
一、問題現象1、多次進出需要強制橫屏的app,比如Real FootBa