android - react native : Resize custom UI component

标签 android react-native

我构建了一个非常简单的原生 Android UI 组件,我想在单击我的 React Native 项目中的按钮时更新其 subview 的大小。更准确地说,当单击此按钮时,我会向我的 SimpleViewManager 发送一条命令,后者又会调用我的自定义 View 的 resizeLayout()

我可以验证 resizeLayout() 是否被正确调用,但布局不会调整大小直到我旋转手机。显然,更改设备的方向会触发我的自定义 View 的 draw(),但我显式调用的 invalidate() 也会触发。

其他布局更改(例如更改背景颜色而不是调整大小)效果很好。

我的自定义组件如下所示:

public class CustomComponent extends RelativeLayout {
    public CustomComponent(Context context) {
        super(context);
        init();
    }

    public CustomComponent(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomComponent(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        inflate(getContext(), R.layout.simple_layout, this);
    }

    public void resizeLayout(){
        LinearLayout childLayout = (LinearLayout) findViewById(R.id.child_layout);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) childLayout.getLayoutParams();
        layoutParams.height = 50;
        layoutParams.width= 50;
        childLayout.setLayoutParams(layoutParams);

        invalidate();
    }
}

simple_layout.xml 如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/child_layout"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:background="#ffee11"/>
</RelativeLayout>

如有任何帮助,我们将不胜感激。

最佳答案

经过几天的搜索,我终于在this中找到了答案。 React Native 的 repo 中的旧报告问题。

此解决方案与 ReactPicker.java 中使用的解决方案相同和 ReactToolbar.java .我必须将以下代码放入我的 CustomComponent 中,之后甚至不需要调用 requestLayout()invalidate()。一旦我更新布局的 LayoutParams,更改就会传播。

@Override
public void requestLayout() {
    super.requestLayout();

    // The spinner relies on a measure + layout pass happening after it calls requestLayout().
    // Without this, the widget never actually changes the selection and doesn't call the
    // appropriate listeners. Since we override onLayout in our ViewGroups, a layout pass never
    // happens after a call to requestLayout, so we simulate one here.
    post(measureAndLayout);
}

private final Runnable measureAndLayout = new Runnable() {
    @Override
    public void run() {
        measure(MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY));
        layout(getLeft(), getTop(), getRight(), getBottom());
    }
};

关于android - react native : Resize custom UI component,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39836356/

相关文章:

c# - 如何设置 Visual Studio 2013 以使用 Android 支持库 V7 来使用抽屉导航?

android - ImageView的麻烦

javascript - 访问身份验证-Token生成找不到变量Token的错误

ios - yarn run ios 运行模拟器但不显示应用程序图标

android - Android 是否支持 JDK 6 或 7

java - 如何在 View 持有者中发送然后从 firebase 接收数据

react-native - 无法在 React Native TextInput 组件中输入任何内容

react-native - 在 react 导航 5.x 中禁用导航回登录/注册屏幕

javascript - 如何根据首次启动设置初始屏幕?

android - 以编程方式更改布局高度,如何避免滞后?异步任务