自定义textview 实现圆角一半纯色背景一半透明背景效果
效果图:
image.png实现方法:
package com.example.weiget;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.textview.R;
/**
* <pre>
* author : lzy
* e-mail : zanyang.lin@newbeeair.com
* time : 2017/05/08
* desc :
* </pre>
*/
public class CustomTextView extends LinearLayout {
private TextView tv;
private TextView tvText;
public CustomTextView(Context context) {
this(context, null);
}
public CustomTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
private void initView(Context context) {
LayoutInflater.from(context).inflate(R.layout.custom_textview_layout, this, true);
tv = (TextView) findViewById(R.id.tv_pm25);
tvText = (TextView) findViewById(R.id.tv_text);
}
public void setText(String text) {
tv.setText(text);
}
public void setTextSize(float size) {
tv.setTextSize(size);
tvText.setTextSize(size);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:gravity="center"
android:orientation="horizontal"
android:padding="0dp">
<TextView
android:id="@+id/tv_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"
android:background="@drawable/border_outer"
android:gravity="center"
android:text="室外"
android:textColor="@color/blue_30b7f1"
android:textSize="6sp" />
<TextView
android:id="@+id/tv_pm25"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/tv_text"
android:layout_weight="3"
android:gravity="center"
android:text="54%"
android:textColor="@color/white_ffffff"
android:textSize="6sp" />
</LinearLayout>
drawable :
- 圆角边框
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="15dp" />
<size android:height="30dp" />
<stroke
android:width="0.5dp"
android:color="@color/white_ffffff" />
</shape>
- 圆角纯色背景 左边圆角右边直角
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/white_ffffff" />
<corners
android:bottomLeftRadius="15dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="15dp"
android:topRightRadius="0dp" />
<size android:height="30dp" />
<stroke
android:width="0.5dp"
android:color="#ffffff" />
<padding
android:bottom="0dp"
android:left="3dp"
android:right="2dp"
android:top="0dp" />
</shape>
项目源码: zanyang/CustomTextview
网友评论