![](/img/trans.png)
[英]How to set the align parent right attribute of TextViews in relative layout programmatically
[英]How to align a button in Relative Layout Programmatically?
我正在嘗試以編程方式在相對布局中對齊按鈕,但是每次我使用上述代碼運行時,應用程序都會崩潰。 我究竟做錯了什么? 當我嘗試使用邊距時,我面臨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);
}
}
系統找不到您的自定義UTButton類,這就是其引發異常的原因,請確保在使用該視圖時已聲明正確的程序包
此xml編碼導致問題-:
<e.user.customcomponent.UIButton
style="@style/UIButtonStyle.Action.Data" />
檢查您使用的軟件包名稱與聲明的軟件包名稱相同。
可能是您的按鈕未綁定,所以它導致了調用虛擬方法的嘗試。 嘗試輸出當前btn為空的內容
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.