【发布时间】:2025-12-29 04:45:16
【问题描述】:
我有一个CoordinatorLayout,包括一个LinearLayout 作为我的BottomSheet。还有一个FrameLayout(也是 CoordinatorLayout 的直接子代)。
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/sketchViewGroup"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/bottomSheet"
android:layout_width="match_parent"
android:layout_height="340dp"
android:background="@drawable/rounded_top"
android:backgroundTint="@color/colorBottomSheetBackground"
android:orientation="vertical"
app:behavior_hideable="false"
app:behavior_peekHeight="@dimen/bottomSheetPeekHeight"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/bottomSheetPeekHeight"
android:gravity="center_vertical"
android:orientation="horizontal">
<SeekBar
android:id="@+id/fixedPointsSeekBar"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:max="10"
android:progress="10" />
</LinearLayout>
</LinearLayout>
我使用 Processing.jar 库在 FrameLayout 上绘制内容。
为了做到这一点,我创建了一个片段(在处理中称为 PFragement)并通过 SupportFragmentManager 将其添加到 FrameLayout。 PFragment 将 PApplet 作为构造函数。在 PApplet 中,画布绘制发生并且 width 和 height 被定义。此外,我调整了 PApplet 的大小以填充我的 FrameLayout。
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val sketch = Sketch();
sketchViewGroup.post {
sketch.sketchWidth = sketchViewGroup.width
sketch.sketchHeight = sketchViewGroup.height
// this works:
// sketch.sketchWidth = sketchViewGroup.width -1
// sketch.sketchHeight = sketchViewGroup.height -1
supportFragmentManager.beginTransaction().add(sketchViewGroup.id, PFragment(sketch)).commit();
}
}
}
和
public class Sketch extends PApplet {
int sketchWidth = 100;
int sketchHeight = 100;
public void settings() {
size(sketchWidth, sketchHeight);
}
public void setup(){
background(0);
}
}
现在,连线的事情正在发生:当我将 Sketch 的大小调整到与 FrameLayout 的大小完全相同时,我的 BottomSheet 不再正确展开。但是,如果我减去我的草图大小之一,一切都会按预期工作。有人可以解释一下并告诉如何解决这个问题吗?
sketch.sketchWidth = sketchViewGroup.width
sketch.sketchHeight = sketchViewGroup.height
sketch.sketchWidth = sketchViewGroup.width -1
sketch.sketchHeight = sketchViewGroup.height -1
【问题讨论】:
标签: android processing android-coordinatorlayout bottom-sheet material-components