首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何给出像条形图这样的背景等级

如何给出像条形图这样的背景等级
EN

Stack Overflow用户
提问于 2016-12-02 08:16:23
回答 1查看 73关注 0票数 1

如果我想在TextView中像下面的图像那样进行分级,我应该做什么?有什么主意或解决办法吗?

(分级的宽度-长度取决于TextView中文本的值)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-02 08:21:10

定义如下可绘制资源文件

代码语言:javascript
复制
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="180"
        android:endColor="@color/your_color"
        android:startColor="@color/your_color2"
        android:type="linear" />
</shape>

并将其用作您视图的背景。

代码语言:javascript
复制
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <View
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:id="@+id/view"
        android:background="@drawable/your_gradient"
        />

    <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="30"
    android:id="@+id/percentageTextview"
    />

</FrameLayout>

在你的活动中使用这个

代码语言:javascript
复制
View background = (View) findViewById(R.id.view);
TextView textView = (TextView) findViewById(R.id.percentageTextview);

DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int  screenWidthInPix = displayMetrics.widthPixels;

ViewGroup.LayoutParams params = background.getLayoutParams();
params.width = (screenWidthInPix * Integer.parseInt(textView.getText().toString()))/100;
background.setLayoutParams(params);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40927562

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档