gpt4 book ai didi

android - setCompoundDrawables 对可绘制形状无效

转载 作者:行者123 更新时间:2023-11-30 02:35:57 25 4
gpt4 key购买 nike

我正在尝试使用 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);
}
}

最佳答案

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

关于android - setCompoundDrawables 对可绘制形状无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26556131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com