2.使用LinerLayout,横向排列,指定TextView宽度
解决方案 »
- android wap代理 post xml问题
- 新手请教一个问题,关于android的工程名
- 如何从 main activity跳转到另一个tabactivity?
- 请问:listview安装中文拼音首字母匹配如何实现?
- 求助,在view中,可否动态改变此view包含view的位置
- 请指教下,这样的界面,应该怎么开
- android sqlite高效查询,插入;AutoCompleteTextView
- Android读取Drawable大图片时,内存溢出。
- 请教大家一下,我想在onMenuOpened()方法里通过获取一个Item从而修改它的title 但总是抛出NullPointerException
- 如何从html源码中获取URL链接?
- android手机中目前有哪些分辨率?
- 再次请教高手,凡是高手有经验的一定帮帮忙,解决后我会共享经验吗,谢谢
我用LinerLayout,横向排列,并指定了TextView 的宽度。
运行结果所有TextView 都在同一行,并且内容都是竖排的。不是我所要的“如果一行放不下,则自动换行”的效果。我将TextView的宽度增大,结果是只有第一行显示了,依旧没有换行的效果,前N -1个TextView正常显示,第N个显示了部分,N + 1往后的没有显示。
private final Paint pFont = new Paint();
Rect rect = new Rect();
pFont.getTextBounds("辉", 0, 1, rect);
Log.v(TAG, "height:"+rect.height()+"width:"+rect.width());
我试了下,有效果
android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:verticalSpacing="2dp" android:horizontalSpacing="2dp"
android:stretchMode="columnWidth"
android:numColumns="auto_fit" android:gravity="center"
在这个GridView下面,还有两个Button,是放在LinearLayout中的: <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
style="@android:style/ButtonBar"> <Button
android:id="@+id/search"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/search">
</Button>
<Button
android:id="@+id/back"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/back">
</Button>
</LinearLayout>截图如下:存在两个问题,一是把下面的两个按钮的空间占用了,二是没有实现我想达到的“横向顺序排列,如果一行放不下,则自动换行”,我希望两个TextView的间距最好能相等,而不是像现在这样通过表格对齐的布局方式。我在上面的GridView外层加入了一个
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">截图如下:布局依然有问题,除了上面说的第二点外,还有在GridView和下面的Button之间有很大的空隙。
android:orientation="vertical"
android:background="#ff000000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" >
<ImageView android:id="@+id/mImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="centerInside"/>
<LinearLayout style="@android:style/ButtonBar"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:id="@+id/btn_Send"
android:layout_y= "370dip"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="操作"/>
<Button android:id="@+id/btn_Exit"
android:layout_y= "370dip"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="取消"/>
</LinearLayout> </LinearLayout>
照这样式写试试
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="center"
android:background="@color/pink">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="10dip">
<TextView
android:text="@string/searchcity"
android:textSize="18sp"
android:paddingBottom="10dip"
android:textColor="@color/black"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dip"
android:orientation="horizontal">
<EditText
android:id="@+id/cityedittext"
android:editable="false"
android:textSize="18sp"
android:textColor="@color/black"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingRight="10dip"/>
<Button
android:id="@+id/othercity"
android:text="@string/othercity"
android:textSize="18sp"
android:textColor="@color/black"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"/>
</LinearLayout>
<TextView
android:text="@string/searchword"
android:textSize="18sp"
android:paddingBottom="10dip"
android:textColor="@color/black"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/wordedittext"
android:textSize="18sp"
android:textColor="@color/black"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dip"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="20dip"
android:paddingRight="20dip"
android:paddingTop="15dip">
<TextView
android:text="@string/hotword"
android:textSize="18sp"
android:paddingBottom="10dip"
android:textColor="@color/black"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<GridView
android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:stretchMode="columnWidth"
android:numColumns="auto_fit"
android:gravity="center">
</GridView>
</LinearLayout>
</LinearLayout>
</ScrollView> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
style="@android:style/ButtonBar"> <Button
android:id="@+id/search"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/search">
</Button>
<Button
android:id="@+id/exit"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/back">
</Button>
</LinearLayout>
</LinearLayout>
GridView中的Adapter的代码如下:
public class TextButtonAdapter extends BaseAdapter { private String[] HOT_WORD = new String[]{
"公交车站", "火车站", "酒店", "宾馆", "大厦",
"银行", "医院", "商场", "KTV", "酒吧", "加油站"
};
private SearchActivity mContext; public TextButtonAdapter(SearchActivity c) {
mContext = c;
} public int getCount() {
return HOT_WORD.length;
} public Object getItem(int position) {
return position;
} public long getItemId(int position) {
return position;
} public View getView(int position, View convertView, ViewGroup parent) {
TextView text;
final String value = HOT_WORD[position];
if (convertView == null) {
text = new TextView(mContext);
text.setPadding(2, 2, 2, 2);
text.setTextSize(TEXT_SIZE);
} else {
text = (TextView) convertView;
}
text.setText(value);
text.setTextColor(Color.BLACK);
text.setId(Resources.ID_HOT_WORD | position);
text.setOnClickListener(mContext); return text;
}
}