【问题标题】:Image view over button in tablelayout表格布局中按钮上的图像视图
【发布时间】:2014-01-04 20:22:38
【问题描述】:

我已经设置了一个 TableLayout 并添加了一行以包含一个按钮和一个 ImageView。 ImageView 只是一个色点。我过去曾将样式与 RelativeLayout 一起使用,并将 ImageView 定位在按钮图像上方。我正在开发的新应用程序使用 TableLayout,但是当我尝试使用 ImageView 设置按钮时,它不允许定位。此选项不适用于 TableLayout 吗?

<TableLayout
    android:id="@+id/TB1"
    android:layout_width="250dp"
    android:layout_height="match_parent"
    android:layout_weight="1" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <Button
            android:id="@+id/Scene1"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:background="@drawable/custom_scene_buttons"
            android:text="Scene 1"
            android:textColor="@drawable/white"
            android:textSize="20sp" />

        <ImageView
            android:id="@+id/colordot1"
            android:layout_width="21dp"
            android:layout_height="21dp"
            android:background="@drawable/circle" />
    </TableRow>

这是改变颜色点的代码。

                Resources res = getResources();
        final Drawable drawable = res.getDrawable(R.drawable.circle);
        drawable.setColorFilter(Color.rgb(i, j, k), Mode.SRC_ATOP);
        ImageView img = (ImageView) findViewById(R.id.colordot1);
        img.setBackgroundDrawable(drawable);

这是我尝试过的线性布局选项

                <LinearLayout android:orientation="vertical" >

            <Button
                android:id="@+id/Scene1"
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:background="@drawable/custom_scene_buttons"
                android:drawableRight="@drawable/circle"
                android:text="Scene 1"
                android:textColor="@drawable/white"
                android:textSize="20sp" />

            <ImageView
                android:id="@+id/colordot1"
                android:layout_width="21dp"
                android:layout_height="21dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:background="@drawable/circle" />
        </LinearLayout>

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    编辑:包括整个 TableLayout。我已经使用 drawableRight 让它工作了。您可以通过抓取 Button 并更改其可绘制对象以编程方式更改圆。 Here's a link describing this。下面是我测试过的 TableLayout 的截图。

    <TableLayout
        android:id="@+id/TB1"
        android:layout_width="250dp"
        android:layout_height="match_parent"
        android:layout_weight="1" >
    
    <TableRow
        android:id="@+id/tableRow1" >
    
        <Button
            android:id="@+id/Scene1"
            android:layout_width="50dp"
            android:layout_height="100dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:background="@color/blue"
            android:drawableRight="@drawable/circle"
            android:text="Scene 1"
            android:textColor="@color/white"
            android:textSize="20sp" />
    
        <Button
            android:id="@+id/Scene2"
            android:layout_width="50dp"
            android:layout_height="100dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:background="@color/blue"
            android:drawableRight="@drawable/circle"
            android:text="Scene 2"
            android:textColor="@color/white"
            android:textSize="20sp" />
    
    </TableRow>
    
    <TableRow
        android:id="@+id/tableRow2" >
    
        <Button
            android:id="@+id/Scene3"
            android:layout_width="50dp"
            android:layout_height="100dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:background="@color/blue"
            android:drawableRight="@drawable/circle"
            android:text="Scene 3"
            android:textColor="@color/white"
            android:textSize="20sp" />
    
        <Button
            android:id="@+id/Scene4"
            android:layout_width="50dp"
            android:layout_height="100dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:background="@color/blue"
            android:drawableRight="@drawable/circle"
            android:text="Scene 4"
            android:textColor="@color/white"
            android:textSize="20sp" />
    
    </TableRow>
    
    </TableLayout>
    

    问题是使用 TableLayout 时,每个 TableRow 的子项都是一列。因此,您的按钮和 ImageView 属于不同的列。您在此 TableLayout 中还有其他行吗?列的宽度由该列中宽度最大的视图定义。因此,如果您有另一个 TableRow 并且它的第二个子视图的宽度为 100dp,这将影响您的第二列的宽度,即您的圆形 ImageView。

    为什么不使用 drawableRight 属性。如下修改 Button 并删除您的 ImageView。您还可以修改可绘制的填充。

    <Button
            android:id="@+id/Scene1"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:background="@drawable/custom_scene_buttons"
            android:text="Scene 1"
            android:textColor="@drawable/white"
            android:drawableRight="@drawable/circle"
            android:textSize="20sp" />
    

    【讨论】:

    • 添加 android:drawableRight="@drawable/circle" 后没有显示颜色点我还发布了用于更改颜色点的代码。我有 10 个这样的布局,我可以将 drawable 转换为更改吗?
    • 奇数。我认为您也可以将 Button 和 ImageView 包装到 LinearLayout 中。然后,该 LinearLayout 将是 TableRow 的单个子项和列。
    • 线性布局不会让我选择让按钮顶部的点仅在它旁边。谢谢你的建议。
    • 这不是真的。您可以将 LinearLayout 的方向设置为“垂直”。然后,将圆圈放在按钮之前。
    • 我发布了您的建议,但不知道如何将点放在顶部(按钮内部)或文本右侧。
    猜你喜欢
    • 2021-12-26
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    • 2013-07-12
    • 2013-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多