【发布时间】:2015-01-04 07:40:45
【问题描述】:
请在回答之前完整阅读问题!
假设我有两种观点:
-
first(黄色)在底部 -
second(青色)填充上方视图的其余部分first
父视图的大小是动态的。
当父高度动态设置为第一列中的值时,如何实现下面的预期列 UI?请注意部分可见的视图是如何隐藏而不是裁剪的。
约束
- 任何代码都是不可能的,我知道可以通过以下方式解决问题:
second.setVisibility(second.getHeight() < SECONDS_PREFERRED_SIZE? GONE : VISIBLE)但我无权访问getHeight(),因此解决方案必须是纯 xml。
通过RemoteViews编写 UI 状态的代码可以放宽。 - 基于上述不允许自定义视图,不允许甚至库,只有基本布局管理器 :
FrameLayout,LinearLayout,RelativeLayout,GridLayout
或者作为最后的手段之一:ViewFlipper、ListView、GridView、StackView或AdapterViewFlipper -
layout_width和layout_height是动态的
在示例中,我修复了它,因此很容易尝试不同的变体。上图是layout_height更改为显示值,模拟我要处理的可能性。 - 我用过
TextView,但是应该普遍适用于任何View - 细心的人可能已经注意到,上述限制了Home screen app widgets with
RemoteViews的解决方案。
尝试
我尝试使用 RelativeLayout 和 LinearLayout 实现,但它们都表现为图片上的实际列。
相对布局
<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>
线性布局
<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>
【问题讨论】:
-
“我无法访问 getHeight()”为什么?
-
@mmlooloo:因为
RemoteViews上没有Viewgetter。远程进程(小部件主机)没有反馈,这是一种方式,这就是我只询问布局 xml 的原因。 -
它的 16 加 -24 用于顶部边缘的 8 填充。
-
@danny117 我不确定我是否理解,您能否通过复制我的尝试来测试它。然后进行建议的更改并通过将
layout_height="16dp"更改为图像上的值来比较视觉效果。 -
很明显顶部边缘有填充,该填充如何消失?我也对这个谜题的答案感兴趣。
标签: android android-layout android-widget android-linearlayout android-appwidget