0

私は何か愚かな間違ったことをしていると確信していますが、ここに私の問題があります。Android:プログラムでTextviewを追加して次の行にテキストを折り返しません

基本的に私はcardviewsを使用して履歴リストを作成しようとしています。これらのカードビューでは、自分のカードにすべてのコンテンツを配置するための垂直方向のLinearLayoutがあります。

このLinearLayoutには、タイトルを保持するHorizo​​ntalという向きの別のLinearLayoutがあります。このlinearLayoutの下には、実際の文字列の内容を保持するtextviewがあります。このコンテンツはかなり大きく、何らかの理由で次の行に折り返されません。

は多分これは、構造を理解するビット容易である:

CardViewを

_(垂直)のLinearLayout

_ _(水平)のLinearLayout

_ _ _のTextView

_ _ 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; 
    } 

だから私は多くのものを試してみましたその私は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 

私はこれらのいくつかを一緒に使ってみました。誰かがこれを解決できることを本当に願っています。 Android 5.1では動作しますが、Androidバージョン7では動作しません。

この行でまだ読んでいない場合は、事前に時間と労力をかけてくれてありがとうと思います!

+0

[here](https://stackoverflow.com/a/7254966/8762152)に記載されているように、レイアウトウェイトを1にプログラムで設定しようとしましたか? –

+0

これを試しましたが、何も変わらない –

答えて

1

同じ問題を抱えている人のために申し訳ありません。私は関数(geefIntAsDpは別のヘルパーで書いています)を使ってディップに私のビューの幅と高さを再計算しました。これは正常な整数では正常に動作しますが、wrap_contentまたはmatch_parentで使用することはできません。だから私がする必要があったのはこの変換を削除することだけでした。

関連する問題