UIButton可以同时设置Title和Image,UIButton有两个属性:titleEdgeInsets(top,left,bottom,right)和imageEdgeInsets(top,left,bottom,right),通过设置这两个,就可以实现所有需要的Button的样式
UIButton 的 默认状态下imageEdgeInsets = UIEdgeInsetsMake(0,0,0,0)titleEdgeInsets = UIEdgeInsetsMake(0,0,0,0) 图片在左文字在右,而且整体水平和垂直居中 。比如下面这个图文按钮:
为了最美观一点,可以设置图标与文字间距 。如下图:
设置图片在右文字在左:
设置图片在上,文字在下:
设置图片左对齐:
设置文字右对齐:
设置文字左对齐,图片右对齐:
//对布局控件添加相对属性RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(宽度,高度)
//添加规则,示例 靠父控件最右边
param.addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
//如果相对某个控件
param.addRule(RelativeLayout.LEFT_OF,某个ID号)
//添加控件
addView(imageview,param)
动态的获取和设置ImageView的宽度和高度,参考实例如下:import android.app.Activity
import android.os.Bundle
import android.util.Log
import android.view.ViewGroup.LayoutParams
import android.widget.ImageView
public class PicTest extends Activity {
private final String TAG = "Pictrue Test!!!"
private ImageView image
private int height = 0
private int width = 0
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
image = (ImageView)findViewById(R.id.ImageView01)
//height = image.getHeight()
//width = image.getWidth()
//Log.d(TAG, "height: " + height)
//Log.d(TAG, "width: " + width)
LayoutParams para
para = image.getLayoutParams()
Log.d(TAG, "layout height0: " + para.height)
Log.d(TAG, "layout width0: " + para.width)
para.height = 300
para.width = 300
image.setLayoutParams(para)
Log.d(TAG, "layout height: " + para.height)
Log.d(TAG, "layout width: " + para.width)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)