动画未显示在自定义布局中

时间:2016-10-21 17:41:27

标签: android android-layout listview android-activity view

我创建了一个自定义布局以进入列表视图。我在自定义布局中有动画,但我无法显示它。如果我将它从自定义列表视图中删除,动画就可以工作,但是它显然不在列表视图中!

下面的代码片段显示了我正在调用的内容。我相信错误在于我使用LayoutInflator的方式,但我不确定发生了什么。

的活动:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  

        ArrayAdapter<Group> adapter = new ArrayAdapter<Group>(this, custom_listview_activetethers, R.id.label, currentUser.getGroupList());
        setListAdapter(adapter);

        setContentView(R.layout.activity_tether_list);

        //standard animated image in the layout, works fine but obviously not in the listview
        ImageView mImageViewEmptying = (ImageView) findViewById(R.id.tetherPulseAnimationTest);
        ((AnimationDrawable) mImageViewEmptying.getBackground()).start();

        //custom animated image, does not show
        LayoutInflater inflater = this.getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.custom_listview_activetethers, null);

        ImageView mImageViewFilling = (ImageView) dialogView.findViewById(R.id.tetherPulseAnimation);
        ((AnimationDrawable) mImageViewFilling.getBackground()).start();

布局(包含动画的工作示例):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_tether_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="uk.tether.topcom.tether.TetherListActivity">

    <ListView android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="10dp"/>

    <ImageView
        android:id="@+id/tetherPulseAnimationTest"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginTop="4px"
        android:background="@drawable/tetheredanimation"
        />

</RelativeLayout>

自定义布局(只有未显示的内容是动画):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/profilepic"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginLeft="4px"
        android:layout_marginRight="2px"
        android:layout_marginTop="4px"
        android:src="@drawable/tetherlogo" >
    </ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="257dp"
        android:layout_height="60dp"
        android:text="@+id/label"
        android:layout_marginTop="4px"
        android:textSize="15dp"
        android:paddingLeft="6dip"
        android:paddingRight="6dip"
        android:gravity="center">
    </TextView>

    <ImageView
        android:id="@+id/tetherPulseAnimation"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginTop="4px"
        android:background="@drawable/tetheredanimation"
        />
</LinearLayout>

我花了相当多的时间试图让它发挥作用,但仍然没有运气!我想膨胀和检索视图,然后从该视图调用动画将起作用,但它没有。

任何人都可以看到我出错的地方,因为我感觉很丢失!

编辑: 它的外观示例,显示自定义布局的工作元素以及动画应为红色的位置。还显示左上角的非自定义动画,以排除图像等问题。 Example

1 个答案:

答案 0 :(得分:1)

由于您使用的是硬编码的layout_width值,我冒昧地说,包含动画的ImageView被推离屏幕。 (可以在设备上的Developer选项中打开布局边界进行检查,但这不是重点)。

你真的不应该对这些值进行硬编码,并且相对于彼此或使用权重来布置视图。作为一个简单的修复,尝试将TextView的layout_width设置为0dp,将权重设置为1.这意味着它将占用布局中剩余的可用空间。