2014-01-19 179 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