我已經構建了一個非常簡單的本機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並不是非常有用。添加賞金。 –