自定義textview 實(shí)現(xiàn)圓角一半純色背景一半透明背景效果

自定義textview 實(shí)現(xiàn)圓角一半純色背景一半透明背景效果

效果圖:

image.png

實(shí)現(xiàn)方法:

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末萝喘,一起剝皮案震驚了整個濱河市蜈首,隨后出現(xiàn)的幾起案子绪颖,更是在濱河造成了極大的恐慌,老刑警劉巖苏揣,帶你破解...
    沈念sama閱讀 222,590評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件黄鳍,死亡現(xiàn)場離奇詭異,居然都是意外死亡平匈,警方通過查閱死者的電腦和手機(jī)框沟,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來增炭,“玉大人忍燥,你說我怎么就攤上這事∠蹲耍” “怎么了梅垄?”我有些...
    開封第一講書人閱讀 169,301評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長输玷。 經(jīng)常有香客問我队丝,道長,這世上最難降的妖魔是什么欲鹏? 我笑而不...
    開封第一講書人閱讀 60,078評論 1 300
  • 正文 為了忘掉前任机久,我火速辦了婚禮,結(jié)果婚禮上赔嚎,老公的妹妹穿的比我還像新娘膘盖。我一直安慰自己,他們只是感情好尤误,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,082評論 6 398
  • 文/花漫 我一把揭開白布侠畔。 她就那樣靜靜地躺著,像睡著了一般袄膏。 火紅的嫁衣襯著肌膚如雪践图。 梳的紋絲不亂的頭發(fā)上掺冠,一...
    開封第一講書人閱讀 52,682評論 1 312
  • 那天沉馆,我揣著相機(jī)與錄音码党,去河邊找鬼。 笑死斥黑,一個胖子當(dāng)著我的面吹牛揖盘,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播锌奴,決...
    沈念sama閱讀 41,155評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼兽狭,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了鹿蜀?” 一聲冷哼從身側(cè)響起箕慧,我...
    開封第一講書人閱讀 40,098評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎茴恰,沒想到半個月后颠焦,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,638評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡往枣,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,701評論 3 342
  • 正文 我和宋清朗相戀三年伐庭,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片分冈。...
    茶點(diǎn)故事閱讀 40,852評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡圾另,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出雕沉,到底是詐尸還是另有隱情集乔,我是刑警寧澤,帶...
    沈念sama閱讀 36,520評論 5 351
  • 正文 年R本政府宣布坡椒,位于F島的核電站饺著,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏肠牲。R本人自食惡果不足惜幼衰,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,181評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望缀雳。 院中可真熱鬧渡嚣,春花似錦、人聲如沸肥印。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽深碱。三九已至腹鹉,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間敷硅,已是汗流浹背功咒。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評論 1 274
  • 我被黑心中介騙來泰國打工愉阎, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人力奋。 一個月前我還...
    沈念sama閱讀 49,279評論 3 379
  • 正文 我出身青樓榜旦,卻偏偏與公主長得像,于是被迫代替她去往敵國和親景殷。 傳聞我的和親對象是個殘疾皇子溅呢,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,851評論 2 361

推薦閱讀更多精彩內(nèi)容