如何在没有代码的情况下隐藏Android布局xml中的部分可见视图?

TWi*_*Rob 5 android android-widget android-layout android-linearlayout android-appwidget

请在回答前完整阅读问题!

假设我有两个观点:

  1. first (黄色)在底部
  2. second (青色)填充上面的其余视图 first

父视图的大小是动态的.

当父高度动态设置为第一列中的值时,如何实现以下预期列UI?请注意部分可见视图是如何隐藏而不是裁剪的.

我想要实现的目标

约束

  • 任何代码都不是可能的,我知道可以用以下方法解决问题: second.setVisibility(second.getHeight() < SECONDS_PREFERRED_SIZE? GONE : VISIBLE) 但是我没有访问权限,getHeight()所以解决方案必须是裸xml.
    可以轻松编写仅编写UI状态的代码RemoteViews.
  • 基于以上没有自定义视图是允许的,没有吨甚至图书馆,只是基本的布局管理器:
    FrameLayout,LinearLayout,RelativeLayout,GridLayout
    或者作为最后手段的先进者之一:
    ViewFlipper,ListView,GridView,StackView或者AdapterViewFlipper
  • layout_widthlayout_height动态的
    .在我固定它,所以它很容易尝试不同的变化的例子.上面的图像被layout_height更改为模拟我想要处理的可能性的显示值.
  • 我用过TextView,但它应该普遍适用于任何View
  • 细心的人可能已经注意到,上面的限制解决主屏幕应用部件RemoteViews.

尝试

我尝试使用RelativeLayout和实现LinearLayout,但它们都表现为图片上的实际列.

的RelativeLayout

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="64dp" (actually fill_parent, but fix it for sake of simplicity)
    android:layout_height="fill_parent" (first column on picture)
    android:layout_gravity="center"
    android:background="#666"
    tools:ignore="HardcodedText,RtlHardcoded"
    >
    <TextView
        android:id="@+id/first"
        android:layout_width="wrap_content"
        android:layout_height="16dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:background="#8ff0"
        android:text="First"
        />
    <TextView
        android:id="@+id/second"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/first"
        android:background="#80ff"
        android:text="Second"
        android:gravity="center"
        />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

的LinearLayout

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="64dp" (actually fill_parent, but fix it for sake of simplicity)
    android:layout_height="fill_parent" (first column on picture)
    android:layout_gravity="center"
    android:background="#666"
    android:orientation="vertical"
    tools:ignore="HardcodedText,RtlHardcoded"
    >
    <TextView
        android:id="@+id/second"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#80ff"
        android:text="Second"
        android:gravity="center"
        />
    <TextView
        android:id="@+id/first"
        android:layout_width="wrap_content"
        android:layout_height="16dp"
        android:layout_gravity="right"
        android:background="#8ff0"
        android:text="First"
        />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

And*_*tor 1

如果这是 RemoteViews/小部件,请尝试使用多个布局文件。扩展AppWidgetProvider并重写该onAppWidgetOptionsChanged方法。在此函数中,您需要执行以下操作:

Bundle options = appWidgetManager.getAppWidgetOptions(appWidgetId);
int minHeight = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
Run Code Online (Sandbox Code Playgroud)

然后根据 minHeight 来确定哪种布局最适合 inflate。在制作小部件时,此页面非常有用。