简体   繁体   中英

How to align a button in Relative Layout Programmatically?

我尝试将按钮与相对布局右侧对齐的代码

I am trying to align a Button in a Relative Layout programmatically but the app is crashing every time i run with the above code. What am i doing wrong? I am facing the same problem with LayoutParams when i am trying to use margins.

Logcat

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>

Added UIButton.java File

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类,这就是其引发异常的原因,请确保在使用该视图时已声明正确的程序包

This xml coading is causing problem-:

<e.user.customcomponent.UIButton
            style="@style/UIButtonStyle.Action.Data" />

Check you are using same package name as that you have declared.

May be your button is not bound, so it caused the attempt to invoke the virtual method. Try to output what the current btn is null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM