我有几个自定义View,我在其中创建了自定义的可样式属性,这些属性在xml布局中声明并在视图的构造函数中读入.我的问题是,如果我在xml中定义布局时没有为所有自定义属性提供显式值,我如何使用样式和主题来获得将传递给我View的构造函数的默认值?
例如:
attrs.xml:
<declare-styleable name="MyCustomView">
<attr name="customAttribute" format="float" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
layout.xml(android:为简单起见,删除了标签):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.mypackage" >
<-- Custom attribute defined, get 0.2 passed to constructor -->
<com.mypackage.MyCustomView
app:customAttribute="0.2" />
<-- Custom attribute not defined, get a default (say 0.4) passed to constructor -->
<com.mypackage.MyCustomView />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud) 我试图保存和恢复由按钮表组成的视图层次结构.表中所需的表行和按钮的数量在运行时才知道,并且在我Activity的onCreate(Bundle)方法中以编程方式添加到膨胀的xml布局中.我的问题是:可以使用Android的默认视图保存/恢复实现来保存和恢复最终表吗?
我目前的尝试的一个例子如下.在初始运行时,表按预期构建.当活动被销毁时(通过旋转设备),重建的视图仅显示TableLayout没有子项的空.
引用的xml文件setContentView(int)包括TableLayout添加按钮的空白.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Setup this activity's view.
setContentView(R.layout.game_board);
TableLayout table = (TableLayout) findViewById(R.id.table);
// If this is the first time building this view, programmatically
// add table rows and buttons.
if (savedInstanceState == null) {
int gridSize = 5;
// Create the table elements and add to the table.
int uniqueId = 1;
for (int i = 0; i < gridSize; i++) { …Run Code Online (Sandbox Code Playgroud) 我试图在xml中构建一个LayerDrawable,其中上层偶尔会完全遮盖下层.为了使较低层较小,我使用InsetDrawable来包装另一个drawable,使其小于视图的完整大小.然而,我意外地发现,放置在包含插图的图层顶部的任何图层也都应用了插图.我找不到支持这种行为的文档,并且很困惑为什么会出现这种情况.
在下面的示例中,我创建了一个包含3层的LayerDrawable.底层和顶层包含椭圆形的drawables,用于占据整个视图.中间层是InsetDrawable内部的矩形绘制.代码如下:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="oval" >
<solid android:color="#00ff00" />
</shape>
</item>
<item>
<inset
android:insetBottom="4dp"
android:insetLeft="4dp"
android:insetRight="4dp"
android:insetTop="4dp" >
<shape android:shape="rectangle" >
<solid android:color="#ff0000" />
</shape>
</inset>
</item>
<item>
<shape android:shape="oval" >
<solid android:color="#0000ff" />
</shape>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
setBackgroundDrawable(getResources().getDrawable(drawableId));在我的视图中调用会生成一个绿色椭圆,按预期填充整个视图,红色矩形按预期插入4dp,但顶层的蓝色椭圆也插入4dp并完全在红色矩形的边界内绘制.
我希望蓝色椭圆形能够完全遮盖绿色椭圆形和大部分红色矩形,但它会嵌入红色矩形内部.有没有办法让蓝色圆圈填满视图但仍保持在顶部?
是否有可能以LayoutAnimationController这样的方式覆盖Android :只有View我在其中指定的某些子项ViewGroup才会生成动画?我的目标是根据当前状态选择任意一组子视图,Activity并在同一时间使用相同的动画为它们设置动画,然后在以后选择不同的任意子视图集并执行相同的操作.我希望能够持续这样做直到Activity完成运行.
到目前为止,我已经查看并驳回了几个选项:
startAnimation(Animation)单独调用特定子视图,但不能保证它们将在完全相同的时间开始和结束,特别是如果任意集中的视图数量很大.LayoutAnimationController.getAnimationForView()似乎是最简单的方法,但方法是最终的,不能被覆盖.我一直在摸不着头脑,并且认为我会给Stack Overflow一个机会.