2014-01-19 49 views
-4

首先,我正在尝试this way。然后我被卡住了然后我问问题但是没有人给出任何答案。然后,我尝试使用@ sayed.jalil在this link中建议的另一种方式。但我的应用程序停止工作。如果有人提供第一个链接的答案真的appriciated。如果有人喜欢根据@ sayed.jalil的答案回答我所尝试的选择。对于我的代码如下─应用程序停止工作

的Xml代码 -

<com.info.abc.JustifiedTextView 
android:id="@+id/textview1" 
android:textColor="#FFFFFF"/> 

活动代码 -

JustifiedTextView txtViewEx = (JustifiedTextView) findViewById(R.id.textview1); 
txtViewEx.setText("some text"); 

JustifiedTextView.java -

import android.annotation.SuppressLint; 
import android.content.Context; 
import android.graphics.Color; 
import android.text.SpannableString; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 


public class JustifiedTextView extends WebView { 

    private String core = "<html><body style='text-align:justify;color:rgba(%s);font-size:%dpx;margin: 10px 10px 10px 10px;'>%s</body></html>"; 
    private String textColor = "0,0,0,255"; 
    private String text = ""; 
    private int textSize = 12; 
    private int backgroundColor = Color.TRANSPARENT; 

    public JustifiedTextView(Context context) { 
     super(context); 
     this.setWebChromeClient(new WebChromeClient() { 

     }); 

    } 

    public void setText(String s) { 
     this.text = s; 
     // this.setPadding(10, 10, 10, 10); 
     reloadData(); 
    } 

    @SuppressLint("NewApi") 
    private void reloadData() { 

     // loadData(...) has a bug showing utf-8 correctly. That's why we need 
     // to set it first. 
     this.getSettings().setDefaultTextEncodingName("utf-8"); 

     this.loadData(String.format(core, textColor, textSize, text), 
      "text/html", "utf-8"); 

     // set WebView's background color *after* data was loaded. 
     super.setBackgroundColor(backgroundColor); 
     // Hardware rendering breaks background color to work as expected. 
     // Need to use software renderer in that case. 
     if (android.os.Build.VERSION.SDK_INT >= 11) 
      this.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null); 
    } 

    public void setTextColor(int hex) { 
     String h = Integer.toHexString(hex); 
     int a = Integer.parseInt(h.substring(0, 2), 16); 
     int r = Integer.parseInt(h.substring(2, 4), 16); 
     int g = Integer.parseInt(h.substring(4, 6), 16); 
     int b = Integer.parseInt(h.substring(6, 8), 16); 
     textColor = String.format("%d,%d,%d,%d", r, g, b, a); 
     reloadData(); 
    } 

    public void setBackgroundColor(int hex) { 
     backgroundColor = hex; 
     reloadData(); 
    } 

    public void setTextSize(int textSize) { 
     this.textSize = textSize; 
     reloadData(); 
    } 

} 

Logcat-

01-19 10:43:19.400: E/AndroidRuntime(1482): FATAL EXCEPTION: main 
01-19 10:43:19.400: E/AndroidRuntime(1482): Process: com.info.abc, PID: 1482 
01-19 10:43:19.400: E/AndroidRuntime(1482): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.info.abc/com.info.abc.History}: 
android.view.InflateException: Binary XML file line #24: Error inflating class com.info.abc.JustifiedTextView 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176) 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226) 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at android.app.ActivityThread.access$700(ActivityThread.java:135) 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397) 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at android.os.Handler.dispatchMessage(Handler.java:102) 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at android.os.Looper.loop(Looper.java:137) 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at android.app.ActivityThread.main(ActivityThread.java:4998) 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at java.lang.reflect.Method.invoke(Method.java:515) 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at dalvik.system.NativeStart.main(Native Method) 
01-19 10:43:19.400: E/AndroidRuntime(1482): Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class com.info.abc.JustifiedTextView 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at android.view.LayoutInflater.createView(LayoutInflater.java:603) 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696) 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:758) 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290) 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at android.app.Activity.setContentView(Activity.java:1928) 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at com.info.abc.History.onCreate(History.java:19) 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at android.app.Activity.performCreate(Activity.java:5243) 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140) 
01-19 10:43:19.400: E/AndroidRuntime(1482):  ... 11 more 
01-19 10:43:19.400: E/AndroidRuntime(1482): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet] 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at java.lang.Class.getConstructorOrMethod(Class.java:472) 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at java.lang.Class.getConstructor(Class.java:446) 
01-19 10:43:19.400: E/AndroidRuntime(1482):  at android.view.LayoutInflater.createView(LayoutInflater.java:568) 
01-19 10:43:19.400: E/AndroidRuntime(1482):  ... 23 more 
+0

您可以发布JustifiedTextView的代码吗? – brwngrldev

+0

请为每一个downvote提供评论,以便我知道我的问题有什么不对。谢谢。 –

+0

可能重复[在Textview中对齐文本](http://stackoverflow.com/questions/21178571/justify-text-in-textview) – codeling

回答

3
Caused by: java.lang.NoSuchMethodException: 
<init> [class android.content.Context, interface android.util.AttributeSet] 
01-19 10:43:19.400: E/AndroidRuntime(1482): 
    at java.lang.Class.getConstructorOrMethod(Class.java:472) 

您没有提供自定义视图所需的所有构造函数。
具体来说,添加这个构造函数。阅读文档和教程以了解如何使用属性集:

public JustifiedTextView(Context context, AttributeSet attrs){ 
    super(context, attrs); 
    this.setWebChromeClient(new WebChromeClient() { 
    }); 
} 
+0

我可以为textview在不同的xml中为不同的屏幕尺寸设置不同的文本大小吗? –

+0

为什么你会想要不同的文字大小?当您指定dp值时,系统会照顾到这一点。如果您真的想要不同的文本大小,您可以通过为不同的设备配置创建不同的xml布局(即使用res/layout-swdp600文件夹)。 – Emmanuel