我正在尝试在撰写视图中重用 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>