【问题标题】:Creating a button in java file on android在android上的java文件中创建一个按钮
【发布时间】:2013-04-15 22:29:25
【问题描述】:

我正在制作游戏,目前我有这个 java 文件

    package pap.crowslanding;

    import android.os.Bundle;

    public class Game extends MainActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.tester1);

}

}

使用我的自定义布局 GameView,我尝试将它与我的 xml 文件 tester1 合并

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/cl_bg"
android:gravity="fill_horizontal">

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="32dp"
    android:layout_marginTop="42dp"
    android:text="@string/hello_world" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="31dp"
android:orientation="horizontal" >

<Button
    android:id="@+id/play_button"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@drawable/custom_button"
    android:text="@string/first_button"
    android:textColor="@drawable/text_color_white" />

<Button
    android:id="@+id/sbutton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"      
    android:layout_weight="1"
    android:background="@drawable/custom_button"
    android:text="@string/menu_settings"
    android:textColor="@drawable/text_color_white" />
</LinearLayout>

</RelativeLayout>

<pap.crowslanding.GameView 
 android:id="@+id/GameView" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"></pap.crowslanding.GameView
>

 </RelativeLayout>

现在:

setContentView(R.layout.tester1);

由于某种原因无法正常工作,但是

setContentView(GameView(this));

工作,请帮助

很抱歉,如果这看起来很简单,我还是个新手,但我仍然在琢磨它。感谢您的阅读。

【问题讨论】:

标签: java android layout


【解决方案1】:

您可以创建一个包含 GameView 和 Button 的布局,并将其用作您的内容视图。

在 res/layout 中创建一个文件 main.xml,如下所示:

<?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="vertical" >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button Text" />

    <pap.crowslanding.GameView
        android:id="@+id/game_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

然后,在您的 Activity 中的 onCreate 中,告诉它使用布局作为内容视图。然后,您可以获取对 GameView 和 Button 的引用,添加点击侦听器或诸如此类的东西。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    GameView gameView = (GameView) findViewById(R.id.game_view);
    Button button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            // respond to clicks
        }
    });
}

编辑: 确保您的 GameView 类从其父类实现所有三个构造函数:

public GameView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO
}

public GameView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO
}

public GameView(Context context) {
    super(context);
    // TODO
}

【讨论】:

  • 每当我这样做时,它都会崩溃并且不会为我显示。但是,如果我这样做 setContentView(new GameView(this));它似乎工作
  • 你能粘贴崩溃的堆栈跟踪吗?
  • 啊,我给你发了logcat,可以吗?
  • 啊,你得到了 NoSuchMethodException。您的 GameView 需要实现其超类中的所有三个构造函数,否则布局充气器无法实例化它。 public GameView(Context context, AttributeSet attrs, int defStyle)public GameView(Context context, AttributeSet attrs)public GameView(Context context)
  • 啊,是的!它可以工作,但不幸的是现在,按钮显示,但我有一个空白的灰色/白色屏幕,GameView 应该是
【解决方案2】:

您不能自己绘制按钮。您需要将自定义 View 包装到 XML 布局中并在其中添加 Button。有关示例,请参阅here。如果你真的想在代码中做同样的事情,你可以通过编程来做同样的事情,但你仍然需要将它包装在一个布局中。

【讨论】:

  • 正如我所说,不幸的是我还没有真正精通 android 编程。我想做的就是在运行我的 Game.java 文件时显示一个按钮,任何按钮。我不太确定如何将 GameView.java 与包含按钮的 xml 文件合并
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-25
  • 2015-09-22
  • 2016-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多