2014-01-19 4 views
-4

먼저 시도 중입니다. this way. 그리고 내가 붙어있어 질문을하지만 아무도 어떤 대답을 제공합니다. 그런 다음 @ sayed.jalil에 의해 제안 된 다른 방법을 this link에 시도합니다. 하지만 내 앱이 작동을 멈춘다. 누군가가 첫 번째 링크에 대한 답변을 제공하는 경우 정말 호감을 보였습니다. 그리고 누군가가 대안에 대답하고 싶다면 @ sayed.jalil 대답에 따라 시도했습니다. 들어 그앱 중지 작동

XML은 활동 코드 -

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

코드 - below- 내 코드

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

가능한 [Textify 텍스트 정당화] (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

다른 텍스트 크기에 대해 다른 XML의 다른 화면 크기에 대해 다른 텍스트 크기를 설정할 수 있습니까? –

+0

왜 다른 텍스트 크기를 원하십니까? dp 값을 지정할 때 시스템이이를 처리합니다. 다른 텍스트 크기가 실제로 필요한 경우 다양한 기기 구성 (즉, res/layout-swdp600 폴더 사용)에 대해 다른 xml 레이아웃을 만들면됩니다. – Emmanuel