布局背景在边距上混乱

9

我将尝试为我的视频播放器制作裁剪栏

trimmer_bar.xml

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/thumbsBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        tools:background="@color/trimmerBarSelected">


        <ImageView
            android:id="@+id/leftThumb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription=""
            android:scaleType="fitStart"
            android:src="@drawable/ic_left_thumb" />


        <ImageView
            android:id="@+id/rightThumb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:contentDescription=""
            android:scaleType="fitEnd"
            android:src="@drawable/ic_right_thumb" />


    </RelativeLayout>
    <ImageView
        android:id="@+id/progressIndicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="60dp"
        android:layout_marginTop="3dp"
        android:contentDescription=""
        android:scaleType="centerCrop"
        android:src="@android:drawable/btn_star_big_on" />

</FrameLayout>

activity_video_viewer:

 <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#a8c71c"
        tools:context=".activities.VideoTrimmerActivity">

        <com.tomatedigital.instagram.longstories.ui.TrimmerBar
            android:id="@+id/trimmerBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/trimmerBarSelected"
            />
    </FrameLayout>

它的逻辑非常出色:当用户触摸时,我会检测是否在任何一个拇指上,并在左拇指的情况下调整thumbsBar的左边距,右侧则调整右边距...

由于这两个imageview对齐了边缘,这种行为使它们移动...但实际上移动的是整个RelativeLayout

问题在于即使调整了边距并移动了imageviews,相对布局仍然显示边距区域的背景

忽略整个计算,我使用以下方法设置边距:

this.thumbsBar= (RelativeLayout) findViewById(R.id.thumbsBar);
this.thumbsBarLayout = (LayoutParams) this.thumbsBar.getLayoutParams();
this.thumbsBarLayout.leftMargin = this.leftMargin;
this.thumbsBarLayout.rightMargin = this.rightMargin;
this.thumbsBar.setLayoutParams(this.thumbsBarLayout);

===============================================================

更新:

为了更容易理解,我有这个:

输入图像描述

我应该有这个:

输入图像描述

1个回答

2
com.tomatedigital.instagram.longstories.ui.TrimmerBar具有与您的RelativeLayout相同的背景,并且宽度为match_parent。您没有真正指定布局是如何加载的,我假设它在您的自定义视图中。
为了测试,请更改自定义视图的背景颜色,以查看边距中看到的背景是来自RelativeLayout还是自定义视图。我猜测它来自自定义视图,更改自定义视图中的背景颜色将解决您的问题。

你是对的,它来自自定义视图...我改变了颜色,整个栏的颜色都改变了,现在我有了相反的问题:相对布局背景的蓝色应该在滑块之间...简单不存在。https://stackoverflow.com/questions/50358393/layout-background-color-disappears-on-runtime - Rafael Lima
@RafaelLima,参考问题中关于“工具”的答案解决了那个问题吗?看起来应该是这样的。“tools=”仅用于设计时,不影响运行时。 - Cheticamp

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接