2.使用LinerLayout,横向排列,指定TextView宽度

解决方案 »

  1.   


    我用LinerLayout,横向排列,并指定了TextView 的宽度。
    运行结果所有TextView 都在同一行,并且内容都是竖排的。不是我所要的“如果一行放不下,则自动换行”的效果。我将TextView的宽度增大,结果是只有第一行显示了,依旧没有换行的效果,前N -1个TextView正常显示,第N个显示了部分,N + 1往后的没有显示。
      

  2.   

    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());
      

  3.   

    用GridView,把apidemo里的ImageView换成TextView
      

  4.   

    用GridView,把apidemo里的ImageView换成TextView
    我试了下,有效果
      

  5.   

    我用GridView来布局,代码如下: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下面,还有两个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之间有很大的空隙。
      

  6.   

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        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>
    照这样式写试试
      

  7.   

    第一个问题解决了,第二个还是那个样子,到现在都没解决,有没有高手知道为什么会这样啊。布局的XML如下:<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;
            }
        }