其他分享
首页 > 其他分享> > android-如何使用布局或TextView设置搜索栏缩略图?

android-如何使用布局或TextView设置搜索栏缩略图?

作者:互联网

我希望我的搜索栏看起来像这样…

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'

解决方法:

此方法从您的资源中获取一个drawable,在其上方绘制一些文本并返回新的drawable.您需要做的就是为其提供气泡的资源ID,并在顶部显示您想要的文本.然后,您可以在需要的任何地方传递返回的drawable.

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);
}

复制自..
Combine image and text to drawable

编辑:

函数调用如下所示:mFreqbar.setThumb(writeOnDrawable(R.drawable.fm_radio_tune_position,mytext));
 我将fm_radio_tune_position.png文件保存在res / drawable /中,“ mytext”是我要在该drawable之上编写的字符串

标签:xamarin-android,seekbar,android
来源: https://codeday.me/bug/20191031/1979284.html