我正在尝试以编程方式对齐相对布局中的按钮,但每次使用上面的代码运行时应用程序都会崩溃。我究竟做错了什么?当我尝试使用边距时,我面临与LayoutParams相同的问题。
logcat的
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="e.user.customcomponent.MainActivity"
android:id="@+id/rLayout">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/EditText" />
<e.user.customcomponent.UIButton
style="@style/UIButtonStyle.Action.Data" />
</RelativeLayout>
button_style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="UIButtonStyle">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:tag">UIButton</item>
<item name="android:id">@id/UIButton</item>
<item name="android:layout_below">@id/EditText</item>
<item name="SetText">Save Data</item>
<item name="SetRoundCorners">true</item>
<item name="SetAlignCentre">true</item>
</style>
<item name="UIButton" type="id">UIButton</item>
</resources>
添加了UIButton.java文件
UIButton.java
package e.user.customcomponent;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.content.res.TypedArray;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class UIButton extends android.support.v7.widget.AppCompatButton {
Button btn = findViewWithTag("UIButton");
EditText editText = findViewById(R.id.et);
DatabaseHandler db;
SQLiteDatabase datab;
final ContentValues cv = new ContentValues();
public UIButton(Context context) {
super(context);
}
@SuppressLint("ResourceType")
public UIButton(final Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs,
R.styleable.UIButton);
int count = typedArray.getIndexCount();
try {
for (int i = 0; i < count; ++i) {
int attr = typedArray.getIndex(i);
// the attr corresponds to the title attribute
if (attr == R.styleable.UIButton_SetText) {
String msg = typedArray.getString(attr);
btn.setText(msg);
} else if(attr == R.styleable.UIButton_SetSubmitAction) {
boolean action = typedArray.getBoolean(attr,false);
if(action == true) {
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getContext(),"Button Clicked!",Toast.LENGTH_LONG).show();
}
});
}
} else if (attr == R.styleable.UIButton_SetRoundCorners) {
boolean roundCorners = typedArray.getBoolean(attr,false);
if(roundCorners == true) {
btn.setBackgroundResource(R.drawable.rounded_corners);
GradientDrawable drawable = (GradientDrawable) btn.getBackground();
drawable.setColor(Color.BLUE);
}
} else if (attr == R.styleable.UIButton_SaveData) {
boolean saveData = typedArray.getBoolean(attr,false);
if(saveData == true) {
db = new DatabaseHandler(context,"DemoDB",null,1);
datab = db.getWritableDatabase();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cv.put("data",editText.getText().toString());
long id = datab.insert("demoTable",null,cv);
Toast.makeText(context,String.valueOf(id)+"Data Inserted!",Toast.LENGTH_LONG).show();
}
});
}
} else if (attr == R.styleable.UIButton_SetAlignCentre) {
boolean setAlignCentre = typedArray.getBoolean(attr,false);
if(setAlignCen
tre == true) {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) btn.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
btn.setLayoutParams(params);
}
}
}
}
finally {
typedArray.recycle();
}
}
public UIButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
答案 0 :(得分:0)
系统无法找到您的自定义UTButton类,这就是为什么它的抛出异常确保您在使用该视图时声明了正确的包
答案 1 :(得分:0)
此xml coading导致问题 - :
<e.user.customcomponent.UIButton
style="@style/UIButtonStyle.Action.Data" />
检查您使用的包名是否与您声明的包名相同。
答案 2 :(得分:0)
可能是您的按钮未绑定,因此导致尝试调用虚方法。尝试输出当前btn为空的内容