Android Issues 总结

本文最后更新于:May 7, 2023 pm

[TOC]


DateTimePicker Control

自定义对话框

How to draw rectangle in XML?

We can create a new XML file inside the drawable folder, and add the following code, then save it as rectangle.xml.

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid
android:color="@android:color/transparent" />
<stroke
android:width="2dip"
android:dashWidth="2dp"
android:dashGap="5dp"
android:color="#ff0000"/>
</shape>

To use it inside a layout we would set the android:background attribute to the new drawable shape,like the following code segment.

1
2
3
4
5
6
<ImageView
android:id="@+id/rectimage"
android:layout_height="100dp"
android:layout_width="100dp"
android:src="@drawable/rectangle">
</ImageView>

finally,have a fun!

Link: http://stackoverflow.com/questions/10124919/can-i-draw-rectangle-in-xml

How to change "shape"(in XML) color dynamically?

The "Shape" code in circle2.xml is as like the following segments:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/shape_circle2"
android:shape="oval"
android:useLevel="false" >
<solid
android:color="@android:color/transparent" />
<stroke
android:width="1dp"
android:color="#00ff00"/>
<size
android:width="55dp"
android:height="55dp"/>
</shape>

The code using "Shape" is as follows:

1
2
3
4
5
6
<ImageView
android:id="@+id/circle_img"
android:layout_height="50dp"
android:layout_width="50dp"
android:background="@drawable/circle">
</ImageView>

And we can modify the "Shape" color simply like this:

1
2
3
ImageView imgviewCircle  = (ImageView)findViewById(R.id.circle_img);
GradientDrawable backgroundGradient = (GradientDrawable)imgviewCircle.getBackground();
backgroundGradient.setColor(Color.GREEN);

Note: It must be the attribute android:background of ImageView that use the "Shape" as long as we modify the "Shape" color like that!

Link: http://stackoverflow.com/questions/7164630/how-to-change-shape-color-dynamically

Android设置textView水平居中显示

  • 让textView里面的内容水平居中:android:gravity="center_horizontal"
  • 让textView控件在它的父布局里水平居中:android:layout_gravity="center_horizontal"

Using lists in Android (ListView) - Tutorial

Link: http://www.vogella.com/tutorials/AndroidListView/article.html


Android Issues 总结
https://cgabc.xyz/posts/9a084c02/
Author
Gavin Gao
Posted on
May 23, 2020
Licensed under