表格行背景图片
TableRow background image
是否可以将图像设置为 table 行的背景?
我找到了多种设置颜色的方法,我需要将其设为图像。
如果不是,我将如何实现相同的结果?
下面是我的 table 布局:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/grey"
android:layout_gravity="center">
<TableRow
android:id="@+id/matchRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip"
android:gravity="center"
android:weightSum="100.0">
<TextView
android:id="@+id/teamAName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="TeamA"
android:layout_gravity="center"
android:textColor="@android:color/white"
android:layout_column="1"
android:gravity="right"
android:layout_weight="30"/>
<ImageView
android:id="@+id/teamAIcon"
android:layout_gravity="center"
android:src="@android:drawable/ic_btn_speak_now"
android:layout_column="2"
android:layout_weight="10"/>
<TableLayout>
<TableRow>
<TextView
android:id="@+id/startTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="19 October 2016"
android:layout_column="3"
android:layout_weight="25"/>
/>
</TableRow>
<TableRow>
<TextView
android:id="@+id/endtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="19 October 2016"
android:layout_column="3"
android:layout_weight="25"/>
/>
</TableRow>
</TableLayout>
<ImageView
android:id="@+id/teamBIcon"
android:src="@android:drawable/ic_btn_speak_now"
android:layout_gravity="center"
android:layout_weight="10"
android:layout_column="4"/>
<TextView
android:id="@+id/teamBName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="TeamB"
android:layout_weight="30"
android:layout_gravity="center"
android:gravity="left"
android:textColor="@android:color/white"
android:layout_column="5"/>
</TableRow>
</TableLayout>
问题答案:
有可能:
<TableRow
android:background="@drawable/your_image">
<TextView
android:id="@+id/endtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="19 October 2016"
android:layout_column="3"
android:layout_weight="25"/>
</TableRow>
或:
TableRow tbr1 = (TableRow)findViewById(R.id.tbr_1);
tbr1.setBackground(yourDrawableImage);
根据最后一条评论:
the image is a url to a image on the web, and i need to set it
programatically
<TableRow
android:id="@+id/tbr_1">
<TextView
android:id="@+id/endtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="19 October 2016"
android:layout_column="3"
android:layout_weight="25"/>
</TableRow>
下载外部图片:
public Bitmap getBitmapFromURL(String src) {
try {
java.net.URL url = new java.net.URL(src);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
位图到可绘制对象:
Drawable d = new BitmapDrawable(getResources(), getBitmapFromURL("your image src));
设置背景:
TableRow tbr1 = (TableRow)findViewById(R.id.tbr_1);
tbr1.setBackground(d);
希望对您有所帮助
是否可以将图像设置为 table 行的背景?
我找到了多种设置颜色的方法,我需要将其设为图像。
如果不是,我将如何实现相同的结果?
下面是我的 table 布局:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/grey"
android:layout_gravity="center">
<TableRow
android:id="@+id/matchRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip"
android:gravity="center"
android:weightSum="100.0">
<TextView
android:id="@+id/teamAName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="TeamA"
android:layout_gravity="center"
android:textColor="@android:color/white"
android:layout_column="1"
android:gravity="right"
android:layout_weight="30"/>
<ImageView
android:id="@+id/teamAIcon"
android:layout_gravity="center"
android:src="@android:drawable/ic_btn_speak_now"
android:layout_column="2"
android:layout_weight="10"/>
<TableLayout>
<TableRow>
<TextView
android:id="@+id/startTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="19 October 2016"
android:layout_column="3"
android:layout_weight="25"/>
/>
</TableRow>
<TableRow>
<TextView
android:id="@+id/endtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="19 October 2016"
android:layout_column="3"
android:layout_weight="25"/>
/>
</TableRow>
</TableLayout>
<ImageView
android:id="@+id/teamBIcon"
android:src="@android:drawable/ic_btn_speak_now"
android:layout_gravity="center"
android:layout_weight="10"
android:layout_column="4"/>
<TextView
android:id="@+id/teamBName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="TeamB"
android:layout_weight="30"
android:layout_gravity="center"
android:gravity="left"
android:textColor="@android:color/white"
android:layout_column="5"/>
</TableRow>
</TableLayout>
问题答案:
有可能:
<TableRow
android:background="@drawable/your_image">
<TextView
android:id="@+id/endtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="19 October 2016"
android:layout_column="3"
android:layout_weight="25"/>
</TableRow>
或:
TableRow tbr1 = (TableRow)findViewById(R.id.tbr_1);
tbr1.setBackground(yourDrawableImage);
根据最后一条评论:
the image is a url to a image on the web, and i need to set it programatically
<TableRow
android:id="@+id/tbr_1">
<TextView
android:id="@+id/endtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="19 October 2016"
android:layout_column="3"
android:layout_weight="25"/>
</TableRow>
下载外部图片:
public Bitmap getBitmapFromURL(String src) {
try {
java.net.URL url = new java.net.URL(src);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
位图到可绘制对象:
Drawable d = new BitmapDrawable(getResources(), getBitmapFromURL("your image src));
设置背景:
TableRow tbr1 = (TableRow)findViewById(R.id.tbr_1);
tbr1.setBackground(d);
希望对您有所帮助