我的应用程序中的以下活动是链接到其他活动的菜单:
/**
* Class containg avtivity that sets up a menu
* which then opens other classes
* @author Ross
*
*/
public class Menu extends ListActivity{
//array that holds the list of names of what will be displayed on screen in menu
String classes[] = { "ViewTimesTables", "Practice", "RandomTest","About"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//takes list or array adapter
setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes));
}
/**
* Method that allows a class to be opened through the menu, dependent on what is clicked
*/
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
//**ADD COMMENT**
super.onListItemClick(l, v, position, id);
//Setting string equal to whatever position in the classes array
String className= classes[position];
try {
//Allows class to be opened dependent on what is clicked
Class ourClass = Class.forName("com.example.multapply." + className);
// set up new intent based off class variable
Intent ourIntent = new Intent(Menu.this, ourClass);
//Start the Activity
startActivity(ourIntent);
}
catch (ClassNotFoundException e) {
//log error
e.printStackTrace();
}
}
}
以下是屏幕当前的外观:
我想:
- 略微增加每个菜单项的大小 - 具有交替的配色方案,即一个绿色,然后是白色,而不是灰色 - 在菜单下添加图像(仅用于美观)。 注意:此活动未链接到xml文件。这就是我发现这些变化的原因。
答案 0 :(得分:0)
如果您只能使用xml进行更改,则可以创建自己的行视图xml并将其传递给ArrayAdapter。在其他情况下,您应该实现自己的ListAdapter。它有getView方法,您可以在其中执行您提到的所有操作。请参阅:http://developer.android.com/reference/android/widget/ListAdapter.html,http://www.vogella.com/tutorials/AndroidListView/article.html
答案 1 :(得分:0)
要更改颜色,只需在xml中添加颜色代码(Google HTML颜色代码)
即可例如: 对于按钮:
<Button
android:textColor="#FFFFFF"
android:background="#FFFFFF"
答案 2 :(得分:0)
通过在xml文件中相应地设计布局来创建自定义列表视图。然后设置扩展BaseAdapter或listAdapter的适配器。在getview方法中,膨胀该xml布局并进行所需的更改。 例如,
class CustomAdapter extends BaseAdapter {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder vh;
if(convertView == null) {
vh = new ViewHolder();
convertView = inflater.inflate(R.layout.list_item, null);
//put your code here
convertView.setTag(vh);
}else {
vh = (ViewHolder) convertView.getTag();
}
return convertView;
}