我正在为我正在开发的应用程序采用无边框按钮的概念。到目前为止,我可以在这些问题的帮助下完美地编辑我的按钮:
How to create standard Borderless buttons (like in the design guidline mentioned)?
Android Drawing Separator/Divider Line in Layout?
但是现在我需要显示一个不同的背景图像或某些东西,以强调当按钮未启用时由于用户尚未填充的某些必填字段。
到目前为止我的代码:
<RelativeLayout
android:id="@+id/layoutId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_marginLeft="4dip"
android:layout_marginRight="4dip"
android:background="@android:color/darker_gray"/>
<Button
android:id="@+id/buttonId"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="?android:attr/selectableItemBackground"
android:onClick="onButtonClicked"
android:text="@string/someText"
android:textColor="@color/someColor"
android:textSize="25sp"
android:textStyle="bold" />
</RelativeLayout>
现在,我打算在按钮的背景中放置一个条纹图像,以显示它何时被禁用。通过阅读下面的这个问题,我可以在没有边框的情况下自定义我的按钮。
Standard Android Button with a different color
我尝试使用这个想法来自定义我的无边框按钮,如下面的代码:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true"
android:drawable="?android:attr/selectableItemBackground" >
</item>
<item android:state_enabled="false"
android:drawable="@drawable/stripes"
android:tileMode="repeat" >
</item>
</selector>
但似乎不可能,?android:attr/selectableItemBackground
中的borderless_button_bg.xml
使程序崩溃。
05-28 17:44:54.139: W/dalvikvm(31664): threadid=1: thread exiting with uncaught exception (group=0x40bc61f8)
05-28 17:44:54.154: E/AndroidRuntime(31664): FATAL EXCEPTION: main
05-28 17:44:54.154: E/AndroidRuntime(31664): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Company.productname/com.Company.productname.myActivity}: android.view.InflateException: Binary XML file line #310: Error inflating class <unknown>
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.app.ActivityThread.access$600(ActivityThread.java:128)
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.os.Handler.dispatchMessage(Handler.java:99)
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.os.Looper.loop(Looper.java:137)
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.app.ActivityThread.main(ActivityThread.java:4514)
05-28 17:44:54.154: E/AndroidRuntime(31664): at java.lang.reflect.Method.invokeNative(Native Method)
05-28 17:44:54.154: E/AndroidRuntime(31664): at java.lang.reflect.Method.invoke(Method.java:511)
05-28 17:44:54.154: E/AndroidRuntime(31664): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
05-28 17:44:54.154: E/AndroidRuntime(31664): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
05-28 17:44:54.154: E/AndroidRuntime(31664): at dalvik.system.NativeStart.main(Native Method)
05-28 17:44:54.154: E/AndroidRuntime(31664): Caused by: android.view.InflateException: Binary XML file line #310: Error inflating class <unknown>
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.view.LayoutInflater.createView(LayoutInflater.java:606)
05-28 17:44:54.154: E/AndroidRuntime(31664): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-28 17:44:54.154: E/AndroidRuntime(31664): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:273)
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.app.Activity.setContentView(Activity.java:1835)
05-28 17:44:54.154: E/AndroidRuntime(31664): at com.Company.productname.myActivity.onCreate(TelaCaminhao.java:41)
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.app.Activity.performCreate(Activity.java:4465)
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
05-28 17:44:54.154: E/AndroidRuntime(31664): ... 11 more
05-28 17:44:54.154: E/AndroidRuntime(31664): Caused by: java.lang.reflect.InvocationTargetException
05-28 17:44:54.154: E/AndroidRuntime(31664): at java.lang.reflect.Constructor.constructNative(Native Method)
05-28 17:44:54.154: E/AndroidRuntime(31664): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.view.LayoutInflater.createView(LayoutInflater.java:586)
05-28 17:44:54.154: E/AndroidRuntime(31664): ... 25 more
05-28 17:44:54.154: E/AndroidRuntime(31664): Caused by: android.content.res.Resources$NotFoundException: File res/drawable-ldpi/borderless_button_bg.xml from drawable resource ID #0x7f020000
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.content.res.Resources.loadDrawable(Resources.java:1958)
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.view.View.<init>(View.java:2832)
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.widget.TextView.<init>(TextView.java:558)
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.widget.Button.<init>(Button.java:108)
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.widget.Button.<init>(Button.java:104)
05-28 17:44:54.154: E/AndroidRuntime(31664): ... 28 more
05-28 17:44:54.154: E/AndroidRuntime(31664): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #7: <item> tag requires a 'drawable' attribute or child tag defining a drawable
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:178)
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:867)
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:804)
05-28 17:44:54.154: E/AndroidRuntime(31664): at android.content.res.Resources.loadDrawable(Resources.java:1955)
05-28 17:44:54.154: E/AndroidRuntime(31664): ... 33 more
所以,我的问题是:我需要使用无边框按钮,我需要在按钮被禁用时清除。我有哪些替代方案?
答案 0 :(得分:0)
这就是我设法解决问题的方法:
public class MyScreen extends Activity {
private Button btnNext;
private Drawable background;
btnNext = (Button) this.findViewById(R.id.btnNext);
background = btnNext.getBackground(); //stores the original Drawable characteristic
...
//disable button
if (!canGoNext) {
btnNext.setEnabled(false);
btnNext.setBackgroundResource(R.drawable.gray); //make it gray
}
//enable button
else {
btnNext.setEnabled(true);
btnNext.setBackgroundDrawable(background); //make it selectableItemBackground again
}
}