【发布时间】:2016-10-03 16:37:26
【问题描述】:
我已经构建了一个非常简单的原生 Android UI 组件,我想在点击我的 react native 项目中的按钮时更新其子视图的大小。更准确地说,当单击此按钮时,我会向我的SimpleViewManager 发送一个命令,而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();
}
}
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>
任何帮助将不胜感激。
【问题讨论】:
标签: android react-native