更改ToastModule.java后如何重建native native

时间:2016-09-06 17:28:49

标签: java android react-native

我最近希望有更多的样式来反应ToastAndroid,所以我编辑了ToastModule.java文件,希望它可以工作,但每次我运行我得到旧的Toast View没有我的更改,请帮助我真的需要这个为我的项目

已编辑的代码

package com.facebook.react.modules.toast;

import android.view.Gravity;
import android.widget.Toast;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.graphics.Color;

import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.bridge.UiThreadUtil;

import java.util.Map;

/**
 * {@link NativeModule} that allows JS to show an Android Toast.
 */
public class ToastModule extends ReactContextBaseJavaModule {

  private static final String DURATION_SHORT_KEY = "SHORT";
  private static final String DURATION_LONG_KEY = "LONG";

  private static final String GRAVITY_TOP_KEY = "TOP";
  private static final String GRAVITY_BOTTOM_KEY = "BOTTOM";
  private static final String GRAVITY_CENTER = "CENTER";

  public ToastModule(ReactApplicationContext reactContext) {
    super(reactContext);
  }

  @Override
  public String getName() {
    return "ToastAndroid";
  }

  @Override
  public Map<String, Object> getConstants() {
    final Map<String, Object> constants = MapBuilder.newHashMap();
    constants.put(DURATION_SHORT_KEY, Toast.LENGTH_SHORT);
    constants.put(DURATION_LONG_KEY, Toast.LENGTH_LONG);
    constants.put(GRAVITY_TOP_KEY, Gravity.TOP | Gravity.CENTER_HORIZONTAL);
    constants.put(GRAVITY_BOTTOM_KEY, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
    constants.put(GRAVITY_CENTER, Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
    return constants;
  }

  @ReactMethod
  public void show(final String message, final int duration) {
    UiThreadUtil.runOnUiThread(new Runnable() {
      @Override
      public void run() {
        Toast.makeText(getReactApplicationContext(), message, duration).show();
      }
    });
  }

  @ReactMethod
  public void showWithGravity(final String message, final int duration, final int gravity) {
    UiThreadUtil.runOnUiThread(new Runnable() {
      @Override
      public void run() {
        Toast toast = Toast.makeText(getReactApplicationContext());

        LinearLayout layout = new LinearLayout(getReactApplicationContext());

        layout.setBackgroundColor(Color.parseColor("#FFFFFF"));

        TextView tv = new TextView(this);

        tv.setTextColor(Color.RED);
        tv.setTextSize(14);
        tv.setGravity(Gravity.CENTER_VERTICAL);
        tv.setText(message);

        layout.addView(tv);

        toast.setView(layout);
        toast.setDuration(duration);
        toast.setGravity(gravity, 0, 0);
        toast.show();
      }
    });
  }
}

1 个答案:

答案 0 :(得分:0)

原始解决方案,而不是重建本机ToastModule:将方法放在这样的类中:

public void showToast(String mex) {

    Toast toast = Toast.makeText(this, mex, Toast.LENGTH_SHORT);
    toast.getView().setBackgroundResource(R.drawable.bg_primary_radius);
    toast.getView().findViewById(android.R.id.message).setBackgroundColor(primaryColor);

    // make your other customization

    toast.show();
}