zoukankan      html  css  js  c++  java
  • 控件设置背景或图片后无法对齐

    今天遇到安卓的一个bug,调了半天才解决。

    bug描写叙述:例如以下图,水平放置一个editTextView和一个button,通过android:layout_alignBottom="@+id/ds_bt_done"之类的方法非常easy让两个组件水平对齐


    如今我给button加个颜色(Background):



    尼玛,button的位置怎么变了!!!

    调了好久,确定这个安卓的一个bug,button在设了background或者color之后会变大,而且布局还是用没设background时的大小。


    直接说解决方式吧:

    1.不要设android:layout_alignBottom, 这个是底部对齐的意思,系统都用了错误的数据,你让它底部对齐,肯定是不行的。

    2.通过调整两个空间的高度,用android:layout_height和android:layout_marginTop等属性手动实现水平对齐。

    结果:


    代码:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    
        <Button
            android:id="@+id/ds_bt_done"
            style="@style/SubmitButton.DarkBlue"
            android:layout_width="wrap_content"
            android:layout_height="28dp"
            android:layout_alignParentRight="true"
            android:text="@string/done" />
    
         <com.my.widget.CustomAutoCompleteTextView
             android:drawableLeft="@drawable/ic_search"
                android:drawableRight="@drawable/ic_delete"
             android:hint=""
             android:id="@+id/textview_item_listview_tv_title"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_toLeftOf="@+id/ds_bt_done"
             android:layout_centerVertical="true"
             android:paddingTop="5dp"
             android:paddingBottom="5dp"
             android:gravity="left|center_vertical"
             android:paddingLeft="5dip"
             android:textSize="15sp" />
    
    </RelativeLayout>


    不知道有没有自己主动实现对齐的方法。我的思路是在代码里写个函数获取button当前的实际高度,或者将button的高度又一次设置为原来的高度,只是临时懒得写了。



    给自己备份的布局參数:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
          android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp">
    
    
        <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:id="@+id/ds_rl_sub" >
        <Button
            android:id="@+id/ds_bt_done"
            android:layout_width="wrap_content"
            android:layout_height="28dp"
            style="@style/SubmitButton.DarkBlue"
            android:layout_alignParentRight="true"
            android:text="@string/done" />
    
    
        <com.taketours.widget.CustomAutoCompleteTextView
            android:id="@+id/ds_cac_dest"
            style="@style/CustomAutoCompleteText"
            android:hint="@string/search_destinations"
            android:layout_toLeftOf="@id/ds_bt_done">
        </com.taketours.widget.CustomAutoCompleteTextView>
    
    
        </RelativeLayout>
        <ListView
            style="@style/commonListView"
            android:layout_marginRight="5dp"
            android:layout_alignLeft="@id/ds_rl_sub"
            android:layout_alignRight="@id/ds_rl_sub"
            android:id="@+id/ds_lv_selected"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/ds_rl_sub" >
        </ListView>
    
    
    </RelativeLayout>
    
    <style name="CustomAutoCompleteText">
            <item name="android:layout_width">fill_parent</item>
            <item name="android:layout_height">wrap_content</item>
            <item name="android:gravity">left|center_vertical</item>
            <item name="android:drawableLeft">@drawable/ic_search</item>
            <item name="android:drawableRight">@drawable/ic_delete</item>
            <item name="android:dropDownWidth">match_parent</item>
            <item name="android:paddingLeft">5dp</item>
            <item name="android:paddingTop">2dp</item>
            <item name="android:ellipsize">end</item>
            <item name="android:focusable">true</item>
            <item name="android:focusableInTouchMode">true</item>
            <item name="android:singleLine">true</item>
        </style>
      <style name="SubmitButton">
            <item name="android:textColor">#ffffff</item>
            <item name="android:layout_width">match_parent</item>
            <item name="android:layout_marginLeft">8dp</item>
            <item name="android:layout_marginRight">8dp</item>
            <item name="android:layout_marginTop">10dp</item>
            <item name="android:layout_height">40dp</item>
        </style>
    
     <style name="SubmitButton.DarkBlue">
            <item name="android:background">@color/darkBlue</item>
        </style>
    


  • 相关阅读:
    (转) dedecms中自定义数据模型
    (转)dedecms网页模板编写
    (转)dedecms入门
    (转)浅谈dedecms模板引擎工作原理及自定义标签
    (转)PHP数组的总结(很全面啊)
    (转)echo和print的区别
    (转)dedecms代码详解 很全面
    (转)php 函数名称前的@有什么作用
    (转)PHP正则表达式的快速学习方法
    GIS中mybatis_CMEU的配置方法
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4230204.html
Copyright © 2011-2022 走看看