Android Studio: создание объекта в пользовательском SwipeButton с атрибутами из XML - PullRequest
0 голосов
/ 20 марта 2019
public class SwipeButton extends RelativeLayout {


private ImageView swipeButtonInner;
private float initialX;
private boolean active;
private TextView centerText;
private ViewGroup background;

private Drawable disabledDrawable;
private Drawable enabledDrawable;

private OnStateChangeListener onStateChangeListener;
private OnActiveListener onActiveListener;

private static final int ENABLED = 0;
private static final int DISABLED = 1;

private int collapsedWidth;
private int collapsedHeight;

private LinearLayout layer;
private boolean trailEnabled = false;
private boolean hasActivationState;

public SwipeButton(Context context) {
    super(context);

    init(context, null, -1, -1);
}

public SwipeButton(Context context, AttributeSet attrs) {
    super(context, attrs);

    init(context, attrs, -1, -1);
}

public SwipeButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    init(context, attrs, defStyleAttr, -1);
}

@TargetApi(21)
public SwipeButton(Context context, AttributeSet attrs, int defStyleAttr, int 
defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);

    init(context, attrs, defStyleAttr, defStyleRes);
}

public boolean isActive() {
    return active;
}

public void setText(String text) {
    centerText.setText(text);
}

public void setBackground(Drawable drawable) {
    background.setBackground(drawable);
}

public void setSlidingButtonBackground(Drawable drawable) {
    background.setBackground(drawable);
}

public void setDisabledDrawable(Drawable drawable) {
    disabledDrawable = drawable;

    if (!active) {
        swipeButtonInner.setImageDrawable(drawable);
    }
}

public void setButtonBackground(Drawable buttonBackground) {
    if (buttonBackground != null) {
        swipeButtonInner.setBackground(buttonBackground);
    }
}

public void setEnabledDrawable(Drawable drawable) {
    enabledDrawable = drawable;

    if (active) {
        swipeButtonInner.setImageDrawable(drawable);
    }
}

public void setOnStateChangeListener(OnStateChangeListener 
onStateChangeListener) {
    this.onStateChangeListener = onStateChangeListener;
}

public void setOnActiveListener(OnActiveListener onActiveListener) {
    this.onActiveListener = onActiveListener;
}

public void setInnerTextPadding(int left, int top, int right, int bottom) {
    centerText.setPadding(left, top, right, bottom);
}

public void setSwipeButtonPadding(int left, int top, int right, int bottom) {
    swipeButtonInner.setPadding(left, top, right, bottom);
}

public void setHasActivationState(boolean hasActivationState) {
    this.hasActivationState = hasActivationState;
}

Мой XML: `

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout    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:id="@+id/AnswerRelativeLayout"
    android:layout_height="70dp"
    android:orientation="vertical"
    tools:context="com.example.dynamicobj.FragmentMain">


    <com.example.dynamicobj.SwipeButton
        android:id="@+id/test_btn"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        app:button_background="@drawable/shape_button"
        app:button_image_disabled="@drawable/ic_launcher_background"
        app:button_image_height="60dp"
        app:button_image_width="100dp"
        app:has_activate_state="true"
        app:initial_state="disabled"
        app:inner_text="Termine buchen"
        app:inner_text_background="@drawable/shape_rounded"
        app:inner_text_bottom_padding="18dp"
        app:inner_text_right_padding="200dp"
        app:inner_text_color="@android:color/black"
        app:inner_text_size="16sp"
        app:inner_text_top_padding="18dp" />

    </LinearLayout>`

F

ragmentMain:

 public class FragmentMain extends Fragment {

    Context context;
    View rootView;

    public FragmentMain() {
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(false);
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_main, container, false);

        LinearLayout layout = (LinearLayout) 
    rootView.findViewById(R.id.AnswerRelativeLayout);
        SwipeButton button = new SwipeButton(getContext());
        layout.addView(button);

        return rootView;
        }
    }    

Итак, я получил этот пользовательский класс SwipeButton из Git (com.ebanx.swipebtn.SwipeButton).Теперь я хочу создать объект из SwipeButton с макетом, похожим на макет из XML-файла.Есть ли какой-нибудь метод, где я могу просто дать новой кнопке готовый макет без использования всех этих методов в классе SwipeButton?Позже я собираюсь динамически создавать кнопки, но все они будут одинакового расположения.Помогите пожалуйста?

1 Ответ

0 голосов
/ 20 марта 2019

вы забыли добавить LayoutParams к вашему button

, используйте приведенный ниже код перед добавлением кнопки в макет

button.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT))

и установите LinearLayout высоту wrap_content не 70dp

...