android - Android : what UI choices? 中的新游戏 Activity

标签 android android-layout android-activity

我的 Android 游戏项目需要一个“新游戏” Activity 。该游戏有几个选项,每次开始新游戏时都应询问:棋盘大小和游戏种子。我想以类似于标准“设置” Activity 的形式执行此操作,但在底部添加了“开始”按钮。鉴于每个新游戏都会选择这些东西,所以它们不想隐藏在应用程序的设置 Activity 中。那么解决这个问题的最佳方法是什么?

我调查过的事情:

  1. 使用自定义 PreferenceActivity,因为行为几乎相同。由于 PreferenceActivity 不使用布局 XML 文件,我看不到向其添加按钮的方法。

  2. 包含一个 ListView 和一个 Button 的线性布局。我已经开始实现这个,但我什至找不到向 ListView 添加项目的方法 - 文档中的 ListView 指南当然不是针对像我这样的初学者的!然后列表项需要是交互的以允许选择选项:这看起来需要大量的工作来复制 PreferenceActivity 几乎自己做的事情。

有没有我遗漏的选项?我花了几个小时试图在文档和以前的问题中找到答案,但没有任何进展。如果有人能指出正确的方向,我将不胜感激。

最佳答案

这是给你的一些代码。希望它会有用。 您应该采用第二种方式并使用 LinearLayout(这是 main.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical">
<ListView
 android:id="@+id/lvSimple"
 android:layout_width="match_parent"
 android:layout_height="wrap_content">
</ListView>

<Button // describe your start button here/>
</LinearLayout>

ListView 项目的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="horizontal">
<ImageView
 android:id="@+id/ivImg"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:src="@drawable/ic_launcher">
</ImageView>
<LinearLayout
 android:id="@+id/linearLayout1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1"
 android:orientation="vertical">
<CheckBox
 android:id="@+id/cbChecked"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="CheckBox">
</CheckBox>
<TextView
 android:id="@+id/tvText"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="right"
 android:layout_marginRight="20dp"
 android:text="TextView">
</TextView>
</LinearLayout>
</LinearLayout>

你的MainActivity.java:

public class MainActivity extends Activity {

  // имена атрибутов для Map
  final String ATTRIBUTE_NAME_TEXT = "text";
  final String ATTRIBUTE_NAME_CHECKED = "checked";
  final String ATTRIBUTE_NAME_IMAGE = "image";

  ListView lvSimple;

  /** Called when the activity is first created. */
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // data array
    String[] texts = { "sometext 1", "sometext 2", "sometext 3",
        "sometext 4", "sometext 5" };
    boolean[] checked = { true, false, false, true, false };
    int img = R.drawable.ic_launcher;

    // our data source
    ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>(
        texts.length);
    Map<String, Object> m;
    for (int i = 0; i < texts.length; i++) {
      m = new HashMap<String, Object>();
      m.put(ATTRIBUTE_NAME_TEXT, texts[i]);
      m.put(ATTRIBUTE_NAME_CHECKED, checked[i]);
      m.put(ATTRIBUTE_NAME_IMAGE, img);
      data.add(m);
    }

    String[] from = { ATTRIBUTE_NAME_TEXT, ATTRIBUTE_NAME_CHECKED,
        ATTRIBUTE_NAME_IMAGE };
    // array of IDs of Views
    int[] to = { R.id.tvText, R.id.cbChecked, R.id.ivImg };

    // create adapter
    SimpleAdapter sAdapter = new SimpleAdapter(this, data, R.layout.item,
        from, to);

    // create ListView and set the adapter
    lvSimple = (ListView) findViewById(R.id.lvSimple);
    lvSimple.setAdapter(sAdapter);
  }
}

result

关于android - Android : what UI choices? 中的新游戏 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31107914/

相关文章:

android - Honeycomb/ICS 中的自定义首选项被破坏

java - 屏幕旋转时 onSaveInstanceState() 不起作用

android - 在android上按下按钮时如何打开空白的html页面

android - 将数据从 Activity 传递到已显示的 Fragment

android - 树解析器与流解析器

android - fragment 到 Activity Backstack 返回空白 View

java - 从共享首选项中检索数据

android - Invalidate() 实际上并没有重绘 child

android - SharedPreferences 类型中的方法 getLong(String, long)

android - 如何去除BottomSheetDialog引起的淡入淡出?