无法在弹出活动中创建自定义按钮

时间:2016-01-05 12:28:58

标签: android android-custom-view

我之前制作了自定义按钮,它们工作得很好。但是这个问题有些不对劲。它不占用我指定的drawable。活动布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawable="@drawable/button_continue"
    android:id="@+id/continue_button"
    android:onClick="goToStart"
    android:layout_gravity="center_vertical"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="86dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:id="@+id/dialogue_textView"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />
</RelativeLayout>

自定义按钮(button_continue.xml):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:state_pressed="true"
    android:state_enabled="true"
    android:drawable="@drawable/button_continue_pressed"/>

<item
    android:state_focused="true"
    android:state_enabled="true"
    android:drawable="@drawable/button_continue_focused"/>

<item
    android:state_enabled="true"
    android:drawable="@drawable/button_continue_enabled"/>

</selector>

我不认为这与活动文件有很大关系,但仍然是:

package com.mycompany.whackamole;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import static com.mycompany.whackamole.GameView.*;

public class Pop extends AppCompatActivity{

int width , height ;
TextView textView;
Intent intent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.pop_activity);

    width = getResources().getDisplayMetrics().widthPixels;
    height = getResources().getDisplayMetrics().heightPixels;
    getWindow().setLayout(width , (int)(height * 0.5));

    textView = (TextView) findViewById(R.id.dialogue_textView);
    setScoreText();
    intent = getIntent();
}

public void goToStart(View view) {
    Intent boo = new Intent(this , StartActivity.class);
    startActivity(boo);
}

private void setScoreText() {
    textView.setText(String.valueOf("Time's up! Your score was " + score));
}

}

如果你能提供帮助,那就太好了!

2 个答案:

答案 0 :(得分:1)

这是因为您在布局文件中设置了错误的属性。它应该是android:background而不是drawable。

答案 1 :(得分:1)

在xml文件中更改:

android:background="@drawable/button_continue"

取代:

android:drawable="@drawable/button_continue"