Gio*_*gos 7 android react-native
我已经构建了一个非常简单的原生Android UI组件,我想在单击来自我的本机项目的按钮时更新其子视图的大小.更确切地说,当单击此按钮时,我向我发送命令SimpleViewManager,然后调用resizeLayout()我的自定义视图.
我可以验证是否resizeLayout()已正确调用,但在旋转手机之前不会调整布局大小.显然,更改设备的方向会触发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();
}
}
Run Code Online (Sandbox Code Playgroud)
而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>
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
Gio*_*gos 17
经过几天的搜索,我终于找到了这个旧的报道问题的回答本地的回购.
此解决方案与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());
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2382 次 |
| 最近记录: |