在以上按钮tablelayout图像视图 [英] Image view over button in tablelayout
问题描述
我已经建立了TableLayout并增加了一行,包括一个按钮和一个ImageView的。该ImageView的只是一个色点。我已经使用了风格与RelativeLayout的过去,上面的按钮图像的ImageView的定位。新的应用程序我的工作使用TableLayout,但是当我尝试设置按钮与ImageView的它不会允许定位。这是选项不适用于TableLayout?
I have setup a TableLayout and added a row to include a button and an ImageView. The ImageView is just a color dot. I have used the style in the past with RelativeLayout and position the ImageView above button image. The new app I am working on uses a TableLayout but when I try to setup button with ImageView it will not allow positioning. Is this option not available for 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>
这是code变色点。
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);
下面是我试过的线性布局选项
Here is the linear layout option I tried
<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>
推荐答案
编辑:包括整个TableLayout。我已经得到了这一点使用drawableRight工作。您可以以编程方式更改了一圈,通过抓住按钮并改变其绘制。 <一href=\"http://stackoverflow.com/questions/7380587/how-can-i-set-the-drawableright-resource-via-$c$c\">Here's描述此的链接。下面是TableLayout,我已经测试的截图。
Including entire TableLayout. I've gotten this to work using drawableRight. You can change the circle programmatically, by grabbing the Button and changing its drawable. Here's a link describing this. Below is a screenshot of the TableLayout, which I've tested.
<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的。
The issue is when using a TableLayout, each TableRow's child is a column. So, your button and ImageView belong to different columns. Do you have other rows in this TableLayout? The width of a column is defined by the view with the greatest width in that column. So, if you have another TableRow and its second child view has a width of 100dp, that will affect the width of your second column, ie your circle ImageView.
为什么不使用drawableRight属性来代替。修改按钮如下,并删除您的ImageView的。您还可以修改您的绘制填充。
Why not use drawableRight attribute instead. Modify the Button as follows and remove your ImageView. You can also modify your drawable padding.
<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" />
这篇关于在以上按钮tablelayout图像视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!