java—如何构建一个android小部件,当它被拉伸时,它会显示更多的按钮?

1tu0hz3e  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(421)

我需要启动一个按钮小部件。一旦启动,当我拉伸宽度时,我希望能够再显示两个按钮。与下图类似。我将从widget[a]开始。一旦我伸懒腰,我想看到[b]和[c]。调整大小时,我只想显示单个小部件。我想避免构建两个不同的小部件。我试着根据小部件宽度调整的时间来改变其他两个按钮的可见性。
布局尺寸1*3网格。当我隐藏这两个按钮时,我会在布局中留下两个网格的空白空间。
谢谢你的帮助!
override onappwidgetoptionschanged(context:context?、appwidgetmanager:appwidgetmanager?、appwidgetid:int、newoptions:bundle?{val remoteviews=remoteviews(context?.packagename、r.layout.coffee\u logger\u widget)

val minWidth = newOptions?.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH)
    val maxWidth = newOptions?.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH)
    val minHeight = newOptions?.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT)
    val maxHeight = newOptions?.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT)

    if (minWidth != null) {
        if (minWidth > 100) {
            remoteViews.setViewVisibility(R.id.espresso_button, View.VISIBLE)
            remoteViews.setViewVisibility(R.id.long_button, View.VISIBLE)
        } else {
            remoteViews.setViewVisibility(R.id.espresso_button, View.GONE)
            remoteViews.setViewVisibility(R.id.long_button, View.GONE)
        }
    }

    appWidgetManager?.updateAppWidget(appWidgetId, remoteViews)

}

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/widget_layout"
android:orientation="vertical"
android:gravity="center"
android:animateLayoutChanges="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:padding="@dimen/widget_margin">

<TextView style="@style/WidgetButtonVerticalSpace" />

<TextView
    android:id="@+id/appwidget_text"
    style="@style/WidgetTextView.Big" />

<TextView
    style="@style/WidgetTextView"
    android:text="@string/grams" />

<TextView style="@style/WidgetButtonVerticalSpace" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView style="@style/WidgetButtonHorizontalSpace" />

    <LinearLayout
        android:id="@+id/ristretto_button"
        style="@style/WidgetBeverageButton" >

        <ImageView
            style="@style/WidgetButtonImage"
            android:src="@drawable/ic_ristretto" />
        <TextView
            style="@style/WidgetButtonText"
            android:text="@string/ristretto_short" />
    </LinearLayout>

    <TextView style="@style/WidgetButtonHorizontalSpace" />

    <LinearLayout
        android:id="@+id/espresso_button"
        android:visibility="gone"
        style="@style/WidgetBeverageButton"
        tools:visibility="visible">

        <ImageView
            style="@style/WidgetButtonImage"
            android:src="@drawable/ic_espresso" />
        <TextView
            style="@style/WidgetButtonText"
            android:text="@string/espresso_short" />

    </LinearLayout>

    <TextView style="@style/WidgetButtonHorizontalSpace" />

    <LinearLayout
        android:id="@+id/long_button"
        android:visibility="gone"
        style="@style/WidgetBeverageButton"
        tools:visibility="visible">

        <ImageView
            style="@style/WidgetButtonImage"
            android:src="@drawable/ic_long_coffee" />
        <TextView
            style="@style/WidgetButtonText"
            android:text="@string/long_coffee_short" />

    </LinearLayout>

    <TextView style="@style/WidgetButtonHorizontalSpace" />

    <!-- Buttons go here -->

</LinearLayout>

<TextView style="@style/WidgetButtonVerticalSpace" />

<TextView
    android:id="@+id/coffee_quote"
    style="@style/WidgetQuote" />

vaqhlq81

vaqhlq811#

你需要设置b和c visibilityGONE ,这将使它们从布局中“消失”,以便它可以缩小到只包含 A .
顺便说一下,如果你加上 animateLayoutChanges = "true" 对于该布局的xml,按钮将很好地淡入淡出,布局将伸缩。您可能需要更改您的订单 B 以及
C VISIBLE 或者 GONE 使它看起来正确,取决于你在做什么
哦,这也取决于你的布局-它需要 layout_width = "wrap_content" 所以它可以收缩到只包裹剩下的部分 A . 如果你用的是 GridLayout 对于固定大小,您需要在代码中更改宽度,或者使用 LinearLayout 或者 ConstraintLayout . 这取决于你到底在做什么!

相关问题