简体   繁体   中英

Android: how to set background image in table layout

Im using tablelayout to display items from sqlite db. I set a backgrpund image for table layout, but the image appears below the texts I need to display the image back to the texts, not below,ie, I need the image frame as 0,0 What I have to do . Now I used

.setbackgroundresources(r.drawable.imagename)

Please guide me I have to add background image for table layout programmatically

If you're setting the background resource for a layout and the image appears below the layout itself (ie. below all the components), you're probably not adding the background resource to the correct layout.

But since you're not really giving much info, that might not even be the case.

Could it be that your image is actually much smaller than the table itself and instead of expanding to the size of the table, could it be staying at its fixed size (which would probably be centered in the layout)?

Try setting the background of the table to a fixed color (ie. view.setBackgroundColor(Color.BLACK);) to test that out. If the entire table fills as you expected, then it's probably related to the image you're using.

If the color fills only the same area the image resource did before, then it's probably my first thought where you're not adding the resource to the proper layout.

then try to put one outer layout linear layout of table layout , in your .xml file and then set the background of that linear layout like

<LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/bkg1">

 <TableLayout
                android:id="@+id/linearLayout1_tblLayout1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <TableRow
                    android:id="@+id/linearLayout1_tblLayout1_tableRow1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center" >


                    ......

                   </TableRow>

</TableLayout>
</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM