无法在弹出窗口中创建自定义按钮 activity
Not able to create Custom button in pop up activity
我以前制作过自定义按钮,它们工作得很好。但是这个有些不对劲。它不占用我指定的可绘制对象。 Activity 布局文件:
<?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>
我认为它与 activity 文件没有多大关系,但仍然 :
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));
}
}
如果你能提供帮助,那就太好了!
这是因为你在布局文件中设置了错误的属性。它应该是 android:background 而不是可绘制的。
在您的 xml 文件中更改:
android:background="@drawable/button_continue"
代替:
android:drawable="@drawable/button_continue"
我以前制作过自定义按钮,它们工作得很好。但是这个有些不对劲。它不占用我指定的可绘制对象。 Activity 布局文件:
<?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>
我认为它与 activity 文件没有多大关系,但仍然 :
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));
}
}
如果你能提供帮助,那就太好了!
这是因为你在布局文件中设置了错误的属性。它应该是 android:background 而不是可绘制的。
在您的 xml 文件中更改:
android:background="@drawable/button_continue"
代替:
android:drawable="@drawable/button_continue"