0

나는 뭔가 바보 같은 짓을하고 있다고 확신하지만 여기에 내 문제가 있습니다.Android : 프로그래밍 방식으로 Textview 다음 줄에 텍스트 배치 안 함

기본적으로 나는 cardviews를 사용하여 히스토리 목록을 만들려고합니다. 이러한 카드보기에는 세로 방향의 LinearLayout이있어서 내 모든 콘텐츠를 카드에 넣을 수 있습니다.

이 LinearLayout에는 타이틀을 보유하고있는 수평 Horizontal이있는 또 다른 LinearLayout이 있습니다. 이 linearLayout 아래에는 실제 문자열 내용을 보유하고있는 textview가 있습니다. 이 콘텐츠는 상당히 크고 다음 줄로 넘어 가지 않습니다.

아마도 이것은 구조 이해 비트 쉽다 :

CardView을

_ (세로)의 LinearLayout

_ _ (가로)의 LinearLayout

_ _ _ 텍스트 뷰

_ _ TextView (문제가있는 것)

미리 인쇄해야하는 카드의 수를 모르기 때문에 프로그래밍 방식으로 이러한 구성 요소를 설정합니다.

은 XML이 : :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="url.Fragments.History_Fragment"> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="start"> 

     <LinearLayout 
      android:id="@+id/historiekLijst_Layout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="start" 
      android:layout_marginRight="5dp" 
      android:orientation="vertical"> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_margin="5dp" 
       android:gravity="start" 
       android:text="Historiek" 
       android:textStyle="bold" 
       android:textSize="25dp"/> 


     </LinearLayout> 
    </ScrollView> 
</LinearLayout> 

이 History_Fragment 클래스 :

LinearLayout linearLayout = (LinearLayout) historiekView.findViewById(R.id.historiekLijst_Layout); 
     CardView card = DynamicComponent_Helper.makeNewCardview(5, Gravity.CENTER_HORIZONTAL,Gravity.CENTER , 5, 15, getContext()); 

    TextView title = DynamicComponent_Helper.makeNewTextView(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 5, Gravity.START, Gravity.START, 0, 20, "title",getContext()); 
         title.setTypeface(null, Typeface.BOLD); 
    TextView content= DynamicComponent_Helper.makeNewTextView(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 5, Gravity.START, Gravity.START, 0, 15, "content which is too long and doesn't get wrapped",getContext()); 

LinearLayout titleLayout = DynamicComponent_Helper.makeNewLinearLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.NO_GRAVITY, Gravity.NO_GRAVITY, LinearLayout.HORIZONTAL, getContext()); 
      LinearLayout cardLayout = DynamicComponent_Helper.makeNewLinearLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL, Gravity.START, LinearLayout.VERTICAL, getContext()); 

      titelLayout.addView(title); 
      cardLayout.addView(titleLayout); 
      cardLayout.addView(content); 
      card.addView(cardLayout); 
      linearLayout.addView(card); 

내 componentCreator_helper가 :

public static CardView makeNewCardview(int margin, int gravity,int layoutGravity, int radius, int padding, Context context){ 
// Initialize a new CardView 
     CardView card = new CardView(context); 

     // Set the CardView layoutParams 
     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.MATCH_PARENT, 
       LinearLayout.LayoutParams.WRAP_CONTENT 
     ); 
     params.setMargins(geefDPAlsInt(margin, context), geefDPAlsInt(margin, context), geefDPAlsInt(margin, context), geefDPAlsInt(margin, context)); 
     params.gravity = layoutGravity; 
     card.setLayoutParams(params); 
     // Set CardView corner radius 
     card.setRadius(geefDPAlsInt(radius, context)); 

     // Set cardView content padding 
     card.setContentPadding(geefDPAlsInt(padding, context), geefDPAlsInt(padding, context), geefDPAlsInt(padding, context), geefDPAlsInt(padding, context)); 

     // Set a background color for CardView 
     card.setCardBackgroundColor(Color.parseColor("#FFFFFF")); 

     // Set the CardView maximum elevation 
     card.setMaxCardElevation(15); 

     // Set CardView elevation 
     card.setCardElevation(9); 

     return card; 

    } 
public static LinearLayout makeNewLinearLayout(int width, int length, int gravity, int layoutGravity, int orientation, Context context){ 
     LinearLayout linearLayout = new LinearLayout(context); 
     LinearLayout.LayoutParams layoutLinearParams = 
       new LinearLayout.LayoutParams(geefDPAlsInt(width, context), geefDPAlsInt(length, context)); 
     layoutLinearParams.gravity = layoutGravity; 
     linearLayout.setOrientation(orientation); 
     linearLayout.setGravity(gravity); 
     linearLayout.setLayoutParams(layoutLinearParams); 

     return linearLayout; 
    } 
    public static ImageView makeNewImageView(String imageSource, int width, int length, int margin, int gravity, int layoutGravity, int tag, Context context){ 
     ImageView imageView = new ImageView(context); 
     LinearLayout.LayoutParams imageLayout = new LinearLayout.LayoutParams(geefDPAlsInt(width, context), geefDPAlsInt(length, context)); 
     imageLayout.gravity = layoutGravity; 
     imageLayout.setMargins(geefDPAlsInt(margin, context), geefDPAlsInt(margin, context), geefDPAlsInt(margin, context), geefDPAlsInt(margin, context)); 
     imageLayout.gravity = gravity; 
     imageView.setLayoutParams(imageLayout); 
     imageView.setImageResource(context.getResources().getIdentifier(imageSource, "drawable", context.getPackageName())); 
     imageView.setTag(tag); 

     return imageView; 
    } 
    public static TextView makeNewTextView(int width, int length, int margin, int gravity, int layoutGravity, int tag, int textsize, String tekst, Context context){ 
     TextView tekstView = new TextView(context); 
     LinearLayout.LayoutParams layoutTextParams = new LinearLayout.LayoutParams(geefDPAlsInt(width, context), geefDPAlsInt(length, context)); 
     layoutTextParams.setMargins(geefDPAlsInt(margin, context), geefDPAlsInt(margin, context), geefDPAlsInt(margin, context), geefDPAlsInt(margin, context)); 
     layoutTextParams.gravity = layoutGravity; 
     tekstView.setLayoutParams(layoutTextParams); 
     tekstView.setGravity(gravity); 
     tekstView.setText(tekst); 
     tekstView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textsize); 
     tekstView.setTag(tag); 
     return tekstView; 
    } 

그래서 나는 물건을 많이 시도 여기

코드입니다 그쪽으로 T는 stackoverflow 및 인터넷에서 찾았어요. 많이 찾았지만 작동하지 않은 몇 가지 사항은 다음과 같습니다.

content.setMaxWidth(0) => my cards have no more content and the height becomes WAY to big 

content.setWidth(0) => same result 
content.setInputType(InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE); => no change 
content.setSingleLine(false); => no change 
content.setMaxLines(10) => no change 
content.setHorizontallyScrolling(false); => no change 

나는 이것들을 함께 시험해 보았습니다. 나는 누군가가 이것을 고칠 수 있기를 정말로 희망한다. 오, 안드로이드 5.1에서는 작동하지만 안드로이드 버전 7에서는 작동하지 않습니다.

이 라인에서 계속 읽고 계시다면, 사전에 시간과 노력을 보내 주셔서 감사드립니다.

+0

[여기] (https://stackoverflow.com/a/7254966/8762152)에 설명 된대로 프로그래밍 방식으로 레이아웃 가중치를 1로 설정해 보았습니까? –

+0

지금 시도했지만 아무것도 변경되지 않습니다. –

답변

1

똑같은 문제가있는 사람이라면 누구나 신청할 수 있습니다. 나는 함수 (geefIntAsDp는 다른 도우미로 작성했습니다.)로 dip의 크기와 높이를 재 계산했습니다. 이 정상적인 정수 함께 잘 작동하지만 wrap_content 또는 match_parent 함께 사용할 수 없습니다. 그래서 내가해야 할 일은이 전환을 삭제하는 것뿐이었습니다.

관련 문제