【问题标题】:setCompoundDrawables on drawable shape with no effect可绘制形状上的 setCompoundDrawables 无效
【发布时间】:2014-12-20 18:37:56
【问题描述】:

我正在尝试使用 Compound Drawables 为 TextView 设置上划线。 我在 XML 可绘制资源中创建了一个线条形状,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="line">
            <stroke android:width="1dp" android:color="#000000" />
            <solid android:color="#00000000" />
</shape>

现在我正尝试将它作为我的 TextView 的上划线,如下所示:

TextView root=(TextView)findViewById(R.id.root);
Drawable background=getResources().getDrawable(R.drawable.overline);
root.setCompoundDrawables(null,background,null,null);

但它完全忽略了指令并且没有出现上划线。 我也考虑过使用 CompoundDrawablesWithIntrinsicBounds,但这也不起作用。 形状的 XML 是错误的还是我以错误的方式使用该方法? 非常感谢您!

编辑:我尝试使用 XML android:drawableTop 属性,但仍然没有运气。 在这一点上,我认为形状本身显然存在问题。 完整代码在这里:

activity_my.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MyActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/hello"
        android:textSize="30sp"
        android:drawableTop="@drawable/overline"
        android:text="Ciao" />

</RelativeLayout>

overline.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="line">
            <stroke android:width="1dp" android:color="#000000" />
            <solid android:color="#00000000" />
</shape>

我的 java 与 Android Studio 中给出的“Hello World”默认示例完全相同:

package tesi.bordengsberiment;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewTreeObserver;
import android.widget.TextView;

public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

【问题讨论】:

    标签: android xml textview compound-drawables


    【解决方案1】:

    我之前也遇到过同样的问题。你想用
    root.setCompoundDrawablesWithIntrinsicBounds();

    【讨论】:

    • 感谢您的回复!正如我所说,我尝试过,但这条线无论如何都不会出现......我认为这是因为形状声明中有问题,因为互联网上的许多人通过使用 setCompoundsDrawablesWithIntrinsicBounds() 解决了这个问题
    • 您的形状 xml 看起来不错。您能否发布包含您要更新的视图的 xml 文件。另外,您是否在绘制之前将可绘制对象添加到视图中?如果不是,Android 很可能不会重绘您的视图,因为它已经设置好了。
    • 尝试使用xml等效android:drawableTop="@drawable/overline"
    • 我尝试了 drawableTop 属性,但仍然没有效果。我更新了问题,添加了更多信息。
    • 您的布局是否可以正常工作?我认为你可能需要给你的 textview 一个固定的高度和宽度。我会说尝试给它一个固定的 dp 高度和宽度。如果这不起作用,请尝试将 textview 设置为同时填充宽度和高度。让我知道其中任何一个是否有效。 Android 可能在计算图片所需空间时遇到问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 1970-01-01
    • 2018-11-28
    • 2011-12-23
    • 1970-01-01
    相关资源
    最近更新 更多