先上效果图片:
效果图.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);
}
});