Android_给你的View上面添加一条线,使线和View宽度保持一致的实现。

先上效果图片:

效果图.png

Demo很小,就一个XML文件和十几行Java代码。GitHub的传送地址

主要布局代码:

<FrameLayout
           android:layout_width="wrap_content"
           android:layout_height="wrap_content">
           <!-- 底部View -->
           <TextView
               android:id="@+id/last_price"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="$19980"
               android:textColor="#aDD"
               android:textSize="20dp"
               />
           <!-- 底部View上面的一条线 -->
           <View
               android:id="@+id/last_price_line"
               android:layout_width="0dp"
               android:layout_height="2dp"
               android:layout_gravity="center"
               android:background="#828180"
               />
       </FrameLayout>

Java的全部代码:

PS: 在Activity中的OnCreate方法中获取控件的宽高几种办法 下面用到了 其中一种 view.post;

//        获取  view的 id
        mLast_price = (TextView) findViewById(R.id.last_price);
        mLast_price_line = findViewById(R.id.last_price_line);

//        使用 view的  post 方式, 用于在activity的oncreate方法中获取它的宽度和高度
        mLast_price.post(new Runnable() {
            @Override
            public void run() {
//                获取要 textview的宽度
                mLast_price.measure(0, 0);
                int width = mLast_price.getWidth();
//                获取 线 的 layout参数
                ViewGroup.LayoutParams lineParams = mLast_price_line.getLayoutParams();
//                将 textview 的宽度  设置给  线的
                lineParams.width = width;
                mLast_price_line.setLayoutParams(lineParams);
            }
        });
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,540评论 25 708
  • 下午回到家,姐姐正坐在房间里看电视,我走进去站在姐姐面前和姐姐一起看,那天我穿着姐姐之前从广州寄给我的衣服,因为太...
    藤木同学阅读 186评论 0 0
  • 【程序46】题目:两个字符串连接程序
    Xplorist阅读 155评论 0 0
  • 我怕 很怕 丢了你 所以 我迫不及待的想要 和你在一起 所以 我要大胆的追求你 你或许厌烦了我的唠叨 不习惯我的磨...
    我是猴子请来的救兵阅读 146评论 1 1
  • 23岁。 她陪你参加你朋友的婚礼。 婚礼上新郎亲吻新娘。 你搂着身边的她说:我们也结婚吧。 她偏偏挣脱你的怀抱扭过...
    CR7_1996阅读 181评论 2 0