android 布局实例解析 柱状图效果
16lz
2022-04-11
一:LinearLayout+RelativeLayout图表效果
1:每个柱子外面是RelativeLayout里边LinearLayout,使用android:layout_alignParentBottom="true"在父容器底部对齐
用背景颜色控制即可
2:中间文字水平垂直居中:
父容器控制水平居中android:layout_alignParentBottom="true"
自己垂直居中 android:layout_gravity="center_vertical"
3:三个柱子左右两边间距保持一样
只需要在外层水平居中就可以了,外层的宽度等于内容宽度 android:layout_width="wrap_content"
所有布局文件1版
<?xml version="1.0" encoding="utf-8"?>
2版
<?xml version="1.0" encoding="utf-8"?>
后台效果
public class TestChat extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.testchat); SetOil(100,70); } private void SetOil(double maxoval, double currentovl)//maxoval最高刻度,当前油量currentovl { TextView tv = (TextView)findViewById(R.id.firshOilBoxText); tv.setText(currentovl+"T"); LinearLayout ly = (LinearLayout)findViewById(R.id.firshOilBox); RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)ly.getLayoutParams(); RelativeLayout lyf = (RelativeLayout)findViewById(R.id.firshOilF); LinearLayout.LayoutParams lpf = (LinearLayout.LayoutParams)lyf.getLayoutParams(); double precendvalue = (currentovl/maxoval);//先计算所占高度的百度分 System.out.println("百分比:"+precendvalue); int needheight = (int)(precendvalue*lpf.height);//在取得该百分比在当前高度下占有的高度,获取到的单位已经是dp不用再转化了 lp.height = needheight; ly.setLayoutParams(lp); } public int Dp2Px(Context context, float dp) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dp * scale + 0.5f); }}
更多相关文章
- android 使用Activity类布局时怎样让图片居中
- Android开发学习:ImageView的scaletype属性
- 按键及其居中
- Android开发学习:ImageView的scaletype属性
- android 横竖屏等设置
- Android(安卓)TextView 45°倾斜效果
- Android(安卓)TextView文字旋转45°
- Android(安卓)、BlackBerry 文本对齐方式对比
- Android(安卓)TextView的滑动