我已经构建了一个非常简单的本机Android用户界面组件,并且我想在点击我的反应本地项目中的按钮时更新其子视图的大小。更确切地说,点击这个按钮后,我发送一个命令给我的SimpleViewManager
,然后调用我的自定义视图的resizeLayout()
。React Native:调整自定义用户界面组件的大小
我可以验证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>
任何帮助将不胜感激。
谢谢。帮助我解决了在层次结构中未得到更新的定制添加使用java视图的问题。 (只需将你的自定义视图放在容器中,用上面的方法覆盖) – Venryx
实际上,对于我的情况,显然还有一步:添加上述容器视图(我从RelativeLayout继承)后,调用'''container.layout (0,0,宽度,高度);'''就可以了。 – Venryx
这对我有很大帮助,我们的图书馆被卡在宽度:0,height:0,因为显然在ReactNative应用程序中的requestLayout并不是非常有用。添加赏金。 –