2017-11-25 4 views
1

하나의 가로 레이아웃에 두 개의 텍스트 뷰가 있습니다. 첫 번째 텍스트는 일반 텍스트이고 두 번째 텍스트는 다른 색상으로 클릭 할 수 있습니다.화면 크기가 변경 될 때 TextViews의 위치를 ​​동적으로 관리하는 방법

XML

<!--inside rootlayout..--> 

<LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_margin="10dp" 
     android:orientation="horizontal"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:gravity="center_horizontal" 
      android:maxLines="2" 
      android:text="By clicking sign up, you agree to our " 
      android:textColor="@color/black" 
      android:textSize="12sp"/> 

     <TextView 
      android:textStyle="bold" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" 
      android:text="terms and conditions" 
      android:textColor="@color/colorPrimary" 
      android:textSize="12sp" 
      android:clickable="true"/> 

     </LinearLayout> 

그리고 그것은 나에게 큰 대형 스크린에 모습 (위의 4.7 인치), 하지만 화면 크기가 낮은 경우, 두 번째 텍스트 뷰 이상한 얻을 수 있습니다.! 자동으로 첫 번째 텍스트 뷰 아래에 배치하거나 부모 레이아웃 방향을 세로로 만듭니다. !!

여기 보이는 모양입니다.!

enter image description here


업데이트 # 1

ForegroundColorSpan이 변경되지 않습니다 왜!? 내가 설정 한 색상 자원에 관계없이 항상 파란색 또는 검은 색으로 표시됩니다.

private void handleTermsConditions() { 
    SpannableStringBuilder stringBuilder = new SpannableStringBuilder(termsTxt.getText()); 
    stringBuilder.setSpan(new StyleSpan(Typeface.BOLD), 38, 58, 0); 
    int color = ContextCompat.getColor(RegistrationActivity.this, R.color.colorPrimary); 
    ForegroundColorSpan fcs = new ForegroundColorSpan(color); 
    stringBuilder.setSpan(fcs, termsTxt.getText().length() - 20, termsTxt.getText().length(), 
     Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
    ClickableSpan clickableSpan = new ClickableSpan() { 

     @Override 
     public void onClick(View widget) { 
     Toast.makeText(RegistrationActivity.this, "Click", Toast.LENGTH_SHORT) 
      .show(); 
     } 
    }; 

    stringBuilder.setSpan(clickableSpan, 38, 58, Spanned.SPAN_POINT_MARK); 
    termsTxt.setMovementMethod(LinkMovementMethod.getInstance()); 
    termsTxt.setText(stringBuilder); 
    } 
+0

필자는 자동으로 발생한다고 생각합니다. 충분한 공간이 있으면 공간에 따라 다르므로 첫 번째 화면과 같을 것입니다. –

답변

1

에 그것에 대해 뭔가입니다 equirement이 두 텍스트보기를 사용하지 않아도 spannable 문자열 작성기를 텍스트 1 개에 배치하고 clickable 및 color 속성을 넣으면 완료됩니다.

코드 : 여기

TextView textView = findViewById(R.id.tvSample); 
    SpannableStringBuilder stringBuilder =new SpannableStringBuilder(textView.getText()); 
    stringBuilder.setSpan(new ForegroundColorSpan(Color.BLUE),textView.getText().length()-20,textView.getText().length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
    stringBuilder.setSpan(new ClickableSpan() { 
     @Override 
     public void onClick(final View view) { 
      Toast.makeText(MainActivity.this,"Click",Toast.LENGTH_LONG).show(); 
     } 
    },textView.getText().length()-20,textView.getText().length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
    textView.setText(stringBuilder); 

This가 하나의 텍스트보기에 두 스팬을 설정하는 방법입니다 텍스트보기에 다른 스팬 퍼팅의 example입니다

+0

'ForegroundColorSpan'이 변경되지 않는 이유는 무엇입니까? 내 res 파일의 색상을 사용했지만 항상 파란색으로 표시됩니까? 다음은 코드'int color = getResources(). getColor (R.color.colorPrimary); foregroundColorSpan fcs = new ForegroundColorSpan (color); stringBuilder.setSpan (fcs, termsTxt.getText(). length() - 20, termsTxt.getText() .length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); –

+0

int color = ContextCompat.getColor (context, R.color.your_color); –

+0

여전히 같은 ..! 업데이트 # 1에 전체 코드를 게시했습니다. 한번 봐주세요. –

0

어느 Fragments

또는

이 autoSizeText을 시도 사용

다음
<TextView 
....... 
android:autoSizeTextType="uniform" 
................................../> 

은 연구에 대한 안드로이드 개발자 site

1

당신은 텍스트 뷰 글꼴 크기를 설정할 수 있습니다 또는 값 폴더를 사용하여 화면 크기에 따라 너비. Try like this.

2
DisplayMetrics displayMetrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 
int height = displayMetrics.heightPixels; 
int width = displayMetrics.widthPixels; 

getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); 

에서 장치의 높이와 너비를 가져 오기 및 세로 여부가 화면을 설정할지 여부를 결정하는 값을 사용

if ((height == <value>) && (width == <value>)) { 
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
} 

* 느낌 귀하의 활동에 필요한대로 자유롭게 수정하십시오

1

가장 쉬운 방법 을 사용할 수 있습니다 0 Android Spannable 속성입니다. 그런 식으로 단일 텍스트 뷰로이 작업을 수행 할 수 있으며 클릭 이벤트를 관리 할 수 ​​있습니다.

여기에이 코드가 있습니다.

public void setHighLightedText(TextView textView, String textToHighlight) { 
    String tvt = textView.getText().toString(); 
    int ofe = tvt.indexOf(textToHighlight, 0); 

    ClickableSpan clickableSpan = new ClickableSpan() { 
     @Override 
     public void onClick(View textView) { 
//   here you do all stuff 
     } 

     @Override 
     public void updateDrawState(TextPaint ds) { 
      super.updateDrawState(ds); 
      ds.setColor(0xff0000ff); 
      ds.setUnderlineText(true); 
// Here you can put your style on textview. 
     } 
    }; 

    SpannableString wordtoSpan = new SpannableString(textView.getText()); 

    for (int ofs = 0; ofs < tvt.length() && ofe != -1; ofs = ofe + 1) { 
     ofe = tvt.indexOf(textToHighlight, ofs); 
     if (ofe == -1) 
      break; 
     else { 
      wordtoSpan.setSpan(clickableSpan, ofe, ofe + textToHighlight.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
      textView.setText(wordtoSpan, TextView.BufferType.SPANNABLE); 
      textView.setMovementMethod(LinkMovementMethod.getInstance()); 
     } 
    } 
} 

당신은 onClick 방법을 볼 수 있습니다. 이 코드를 Utility 클래스에 넣으면 클릭 또는 콜백을 설정할 수 있습니다.

보너스 또한이이 작업을 수행 할 수있는 권리 방법입니다.

관련 문제