ListActivity全屏方法
ListActivity Full Screen Method
我创建了一个 7 天的列表,并希望该列表占据全屏。
但这份名单在底部有一个缺口。
我想消除差距。
我的代码如下:
package com.example.collegehack;
导入android.app.ListActivity;
import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Days extends ListActivity {
String days[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday", "Sunday" };
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Days.this,
android.R.layout.simple_list_item_1,days));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String temporary = days[position];
try {
Class ourClass = Class.forName("com.example.collegehack"
+ temporary);
Intent ourIntent = new Intent(Days.this, ourClass);
startActivity(ourIntent);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
请帮我解决这个问题。
提前致谢。
如果您的列表不需要展开或向下滚动并且行数总是有限的(即一周中的几天),请考虑根本不使用 ListView。使用一个 LinearLayout
和七个 TextViews
,并分别设置它们的 android:layout_weight=1
和 android:layout_height="0dp"
LinearLayout 将在 TextView 中均匀分布白色 space。
然后您可以为每个 TextView 设置 setOnClickListener
以执行您要查找的操作。
您可以使用带有自定义列表项的自定义列表视图,如下所示link然后修改单个项目的高度。
为此,您需要扩充自己的视图。
按照以下步骤操作:
1. 创建 my_layout.xml
包含 textView
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/textView1"
android:gravity="center_vertical"
android:layout_centerVertical="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
2. 在您的适配器中,您需要覆盖 getView() 方法并像这样设置行高:
setListAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,days) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.my_layout, parent, false);
convertView.getLayoutParams().height = parent.getHeight() / getCount();
convertView.requestLayout();
TextView tv = (TextView) convertView.findViewById(R.id.textView1);
tv.setText(getItem(position));
}
return convertView;
}
});
3. 禁用列表滚动,因为在此解决方法中分隔线高度不计算在内,您的 listView 将滚动一毫米:
getListView().setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
return true; // Indicates that this has been handled by you
// and will not be forwarded further.
}
return false;
}
});
就是这样!玩得开心:)
您可以使列表的字体更大或使用自定义列表视图,或者如果您的 activity 不需要扩展或滚动,请使用线性布局和其中的一些按钮
我创建了一个 7 天的列表,并希望该列表占据全屏。 但这份名单在底部有一个缺口。 我想消除差距。
我的代码如下:
package com.example.collegehack;
导入android.app.ListActivity;
import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Days extends ListActivity {
String days[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday", "Sunday" };
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Days.this,
android.R.layout.simple_list_item_1,days));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String temporary = days[position];
try {
Class ourClass = Class.forName("com.example.collegehack"
+ temporary);
Intent ourIntent = new Intent(Days.this, ourClass);
startActivity(ourIntent);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
请帮我解决这个问题。 提前致谢。
如果您的列表不需要展开或向下滚动并且行数总是有限的(即一周中的几天),请考虑根本不使用 ListView。使用一个 LinearLayout
和七个 TextViews
,并分别设置它们的 android:layout_weight=1
和 android:layout_height="0dp"
LinearLayout 将在 TextView 中均匀分布白色 space。
然后您可以为每个 TextView 设置 setOnClickListener
以执行您要查找的操作。
您可以使用带有自定义列表项的自定义列表视图,如下所示link然后修改单个项目的高度。
为此,您需要扩充自己的视图。
按照以下步骤操作:
1. 创建 my_layout.xml
包含 textView
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/textView1"
android:gravity="center_vertical"
android:layout_centerVertical="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
2. 在您的适配器中,您需要覆盖 getView() 方法并像这样设置行高:
setListAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,days) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.my_layout, parent, false);
convertView.getLayoutParams().height = parent.getHeight() / getCount();
convertView.requestLayout();
TextView tv = (TextView) convertView.findViewById(R.id.textView1);
tv.setText(getItem(position));
}
return convertView;
}
});
3. 禁用列表滚动,因为在此解决方法中分隔线高度不计算在内,您的 listView 将滚动一毫米:
getListView().setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
return true; // Indicates that this has been handled by you
// and will not be forwarded further.
}
return false;
}
});
就是这样!玩得开心:)
您可以使列表的字体更大或使用自定义列表视图,或者如果您的 activity 不需要扩展或滚动,请使用线性布局和其中的一些按钮