3

我希望我的搜索栏看起来像这样......在此处输入图像描述

TextView 应该随着搜索栏的拇指移动......我试过了

seek_needle.xml

<?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="match_parent"
android:orientation="vertical" >

<TextView 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text=""
    />    
    <ImageView 
        android:layout_width="25dp"
        android:layout_height="fill_parent"
        android:src="@drawable/seek_needle"
        />

</LinearLayout>

并在 mail.xml

  <SeekBar
                android:id="@+id/seekBar1"
                android:layout_width="349dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:max="202"
                android:layout_weight="80"
                android:thumb="@Layout/seek_needle"                  
                  >         </SeekBar>  

因为这个应用程序崩溃
了,请帮帮我......

这是logcat输出...

    09-11 17:50:23.582: I/dalvikvm(4142): threadid=3: reacting to signal 3
09-11 17:50:23.762: I/dalvikvm(4142): Wrote stack traces to '/data/anr/traces.txt'
09-11 17:50:24.062: I/dalvikvm(4142): threadid=3: reacting to signal 3
09-11 17:50:24.212: I/dalvikvm(4142): Wrote stack traces to '/data/anr/traces.txt'
09-11 17:50:24.582: I/dalvikvm(4142): threadid=3: reacting to signal 3
09-11 17:50:24.632: I/dalvikvm(4142): Wrote stack traces to '/data/anr/traces.txt'
09-11 17:50:24.664: D/dalvikvm(4142): GC_CONCURRENT freed 73K, 2% free 9435K/9607K, paused 7ms+8ms
09-11 17:50:24.862: D/AndroidRuntime(4142): Shutting down VM
09-11 17:50:24.872: W/dalvikvm(4142): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
09-11 17:50:24.952: E/AndroidRuntime(4142): FATAL EXCEPTION: main
09-11 17:50:24.952: E/AndroidRuntime(4142): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.audioscreen/com.example.audioscreen.MainActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.audioscreen/com.example.audioscreen.Fm1Activity}: android.view.InflateException: Binary XML file line #63: Error inflating class android.widget.SeekBar
    09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.os.Looper.loop(Looper.java:137)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.ActivityThread.main(ActivityThread.java:4424)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at java.lang.reflect.Method.invokeNative(Native Method)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at java.lang.reflect.Method.invoke(Method.java:511)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at dalvik.system.NativeStart.main(Native Method)
09-11 17:50:24.952: E/AndroidRuntime(4142): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.audioscreen/com.example.audioscreen.Fm1Activity}: android.view.InflateException: Binary XML file line #63: Error inflating class android.widget.SeekBar
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1797)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:682)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.widget.TabHost.setCurrentTab(TabHost.java:346)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.widget.TabHost.addTab(TabHost.java:236)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at com.example.audioscreen.MainActivity.onCreate(MainActivity.java:82)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.Activity.performCreate(Activity.java:4465)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-11 17:50:24.952: E/AndroidRuntime(4142):     ... 11 more
09-11 17:50:24.952: E/AndroidRuntime(4142): Caused by: android.view.InflateException: Binary XML file line #63: Error inflating class android.widget.SeekBar
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.view.LayoutInflater.createView(LayoutInflater.java:606)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.Activity.setContentView(Activity.java:1835)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at com.example.audioscreen.Fm1Activity.onCreate(Fm1Activity.java:24)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.Activity.performCreate(Activity.java:4465)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-11 17:50:24.952: E/AndroidRuntime(4142):     ... 21 more
09-11 17:50:24.952: E/AndroidRuntime(4142): Caused by: java.lang.reflect.InvocationTargetException
09-11 17:50:24.952: E/AndroidRuntime(4142):     at java.lang.reflect.Constructor.constructNative(Native Method)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.view.LayoutInflater.createView(LayoutInflater.java:586)
09-11 17:50:24.952: E/AndroidRuntime(4142):     ... 36 more
09-11 17:50:24.952: E/AndroidRuntime(4142): Caused by: android.content.res.Resources$NotFoundException: File res/layout/seek_needle.xml from drawable resource ID #0x7f030008
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.content.res.Resources.loadDrawable(Resources.java:1923)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.widget.AbsSeekBar.<init>(AbsSeekBar.java:70
09-11 17:50:25.032: D/dalvikvm(4142): GC_CONCURRENT freed 797K, 9% free 9183K/10055K, paused 7ms+7ms
09-11 17:50:25.203: I/dalvikvm(4142): threadid=3: reacting to signal 3
09-11 17:50:25.232: I/dalvikvm(4142): Wrote stack traces to '/data/anr/traces.txt'
09-11 17:50:25.642: I/dalvikvm(4142): threadid=3: reacting to signal 3
09-11 17:50:25.662: I/dalvikvm(4142): Wrote stack traces to '/data/anr/traces.txt'
4

2 回答 2

4

此方法从您的资源中获取一个可绘制对象,在其上绘制一些文本并返回新的可绘制对象。您需要做的就是给它您的气泡的资源 ID,以及您想要在顶部的文本。然后,您可以将返回的可绘制对象传递到您想要的任何地方。

public BitmapDrawable writeOnDrawable(int drawableId, String text){

    Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);

    Paint paint = new Paint(); 
    paint.setStyle(Style.FILL);  
    paint.setColor(Color.BLACK); 
    paint.setTextSize(20); 

    Canvas canvas = new Canvas(bm);
    canvas.drawText(text, 0, bm.getHeight()/2, paint);

    return new BitmapDrawable(bm);
}

复制自..将 图像和文本组合到drawable

编辑:

函数调用看起来像这样,mFreqbar.setThumb(writeOnDrawable(R.drawable.fm_radio_tune_position, mytext)); 我保存了我的fm_radio_tune_position.png文件,res/drawable/' mytext'是我想写在那个可绘制对象之上的字符串

于 2014-03-17T13:26:30.480 回答
-1

为使用 MonoDroid / Xamarin.Android 的人翻译的答案功能如下:

public BitmapDrawable WriteOnDrawable(int drawableId, String text)
    {
        Bitmap bm = BitmapFactory.DecodeResource(Resources, drawableId).Copy(Bitmap.Config.Argb8888, true);

        Paint paint = new Paint();
        paint.SetStyle(Paint.Style.Fill);
        paint.Color = Color.Black;
        paint.TextSize = 20;

        Canvas canvas = new Canvas(bm);
        canvas.DrawText(text, 0, bm.Height / 2, paint);

        return new BitmapDrawable(bm);
    }
于 2016-05-10T13:27:13.880 回答