【问题标题】:Compose AndroidView don't show my customView撰写 AndroidView 不显示我的 customView
【发布时间】:2021-08-06 14:05:43
【问题描述】:

我正在尝试在撰写视图中重用 customView。我遵循指南来实现这一点,但我无法在 DefaultPreview 中显示我的 customView,并且在运行应用程序时没有显示任何内容。

这是我正在尝试的:

@Composable
fun Total() {
    Row(Modifier.padding(top = 24.dp)
        .fillMaxWidth(),
        horizontalArrangement = Arrangement.SpaceBetween) {

        AndroidView(
            factory = { context ->
                // Creates custom view
                CustomView(context).apply {
                    
                }
            },
        )

        Column(modifier = Modifier.align(Alignment.Bottom)) {
            Text("Total",
                modifier = Modifier.fillMaxWidth(),
                textAlign = TextAlign.End,
                fontSize = 12.sp,
                color = colorResource(id = R.color.app_grey_dark))

            Text("1 €",
                modifier = Modifier.fillMaxWidth(),
                textAlign = TextAlign.End,
                fontSize = 18.sp,
                color = colorResource(id = R.color.app_black_dark))
        }
    }
}

Java查看代码:

public class CustomView extends FrameLayout {
    ...
    ...
private Scene myView;

    public CustomView(@NonNull Context context) {
            super(context);
        }
    
        public CustomView(@NonNull Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
        }
    
        public CustomView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        protected void onFinishInflate() {
            super.onFinishInflate();
            screenRoot = findViewById(R.id.scene_root_myView);
            initControlAddToCardOne();
            myView.enter();
            TransitionInflater transitionInflater = TransitionInflater.from(getContext());
            transitionManager = transitionInflater.inflateTransitionManager(R.transition.scene_transition_manager,
                    screenRoot);
        }



   private void initControlAddToCardOne() {
    myView= Scene.getSceneForLayout(screenRoot, R.layout.my_layout, getContext());
myView.setEnterAction(() -> {
                ViewGroup defaultView = myView.getSceneRoot();
   
                });
            });
}

布局xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true">

    <ImageView
        android:id="@+id/img_button"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:src="@drawable/image"
        android:adjustViewBounds="true"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

【问题讨论】:

    标签: android android-jetpack-compose composable


    【解决方案1】:

    AndroidView 需要宽高,使用修饰符:

    AndroidView(
        modifier = Modifier
            .fillMaxWidth()
            .height(100.dp),
        factory = { context ->
            CustomView(context = context)
        },
        update = {
               
        }
    )
    

    【讨论】:

      猜你喜欢
      • 2021-10-13
      • 2012-08-10
      • 2012-12-19
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 2011-06-25
      • 1970-01-01
      相关资源
      最近更新 更多