2017-10-11 3 views
1

私はプログラムで動的にxml定義のテーブルレイアウトを入れる在庫管理Androidアプリをやっています。すべてのアイテムについて、私は2つのビュー:インベントリアイテムの名前を含むテキストビューと、3つのラジオボタンを含むラジオグループ:存在、欠落、および修復が必要なtablerowを作成します。また、テーブルレイアウトの各行の下には、テキストビューとエディットテキストが含まれていますが、その部分に問題がない線形レイアウトがあります。Tablerowの2行目に3ボタンラジオグループの1つのラジオボタンを移動させる

これは、それが現在どのように見えるかを示す私のスクリーンショットです:今 https://preview.ibb.co/k8a7Yw/Screenshot_20171011_170618.png

、ラジオボタンは、テキストようtablelayoutためのXMLにshrinkcolumn = 0を設定した後(自分の携帯電話の画面上で細かい表示します3番目のラジオボタンは途切れることはありません)、項目名は押しつぶされます。私がしたいのは、最後のラジオボタン(修理が必要)を一行下に移動することですが、ラジオグループと行に保持して、ラジオグループから継承するカスタムクラスを作成する必要はありません。

現在のコード:tablelayoutのXMLを

public class PMR { 
    String result; 
    String caption; 
    String repairString; 
    EditText repairEditText; 
    RadioButton pRadioButton; 
    RadioButton mRadioButton; 
    RadioButton rRadioButton; 

    public PMR(){ 
    } 
    public PMR(final TableLayout tableLayout, final TableRow tableRow, 
    final Context context, String caption){ 
     this.caption=caption; 
     result="incomplete"; 
     repairString=null; 
     final RadioGroup radioGroup=new RadioGroup(context); 
     radioGroup.setTag("incomplete"); 
     radioGroup.setOrientation(RadioGroup.HORIZONTAL); 
     tableRow.addView(radioGroup); 

     pRadioButton = new RadioButton(context); 
     pRadioButton.setText("Present"); 
     pRadioButton.setId(generateViewId()); 
     final int pId=pRadioButton.getId(); 

     mRadioButton = new RadioButton(context); 
     mRadioButton.setText("Missing"); 
     mRadioButton.setId(generateViewId()); 
     final int mId=mRadioButton.getId(); 

     rRadioButton = new RadioButton(context); 
     rRadioButton.setText("Repairs Needed"); 
     rRadioButton.setId(generateViewId()); 
     final int rId=rRadioButton.getId(); 

     radioGroup.addView(pRadioButton); 
     radioGroup.addView(mRadioButton); 
     radioGroup.addView(rRadioButton); 

     final LinearLayout textLayout=new LinearLayout(context); 
     textLayout.setOrientation(LinearLayout.HORIZONTAL); 
     tableLayout.addView(textLayout); 
     textLayout.setTag("Text Row"); 

     final TextView labelText = new TextView(context); 
     labelText.setText(""); 
     textLayout.addView(labelText); 

     repairEditText = new EditText(context); 
     repairEditText.setEnabled(false); 
     repairEditText.setText(""); 
     textLayout.addView(repairEditText); 

     final String prefix="Notes: "; 

     radioGroup.setOnCheckedChangeListener(new 
     RadioGroup.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged (RadioGroup group, 
      int checkedId){ 

       if (checkedId!=rId && 
       repairEditText.getText().toString()!=null){ 
        repairString=repairEditText.getText().toString(); 
       } 
       if (checkedId == pId) { 
        result="Present"; 
        repairEditText.setText(""); 
        repairEditText.setEnabled(false); 
        labelText.setText(""); 
        radioGroup.setTag("pTag"); 
       } else if (checkedId == mId) { 
        result="Missing"; 
        repairEditText.setText(""); 
        repairEditText.setEnabled(false); 
        labelText.setText(""); 
        radioGroup.setTag("mTag"); 
       } else if (checkedId == rId) { 
        result="Repairs Needed"; 
        if (repairString!=null){ 
         repairEditText.setText(repairString); 
        } 
        repairEditText.setEnabled(true); 
        repairEditText.requestFocus(); 
        labelText.setText(prefix); 
        radioGroup.setTag("rTag"); 
       } 
       else { 
        result = "incomplete"; 
        radioGroup.setTag("incomplete"); 
       } 
      } 
     }); 
    } 
} 

は: https://image.ibb.co/i5qrtw/goodtablerowpmr.png

+0

https://stackoverflow.com/questions/11891105/have-horizo​​ntal-radiobuttons-wrap-if-too-long -for-screen – HomeIsWhereThePcIs

+0

そのポストの2つの回答のどちらも、提起された問題に答えなかった。最初はラジオグループでは機能せず、2番目のボタンでは部屋の外にある場合にのみ追加のボタンを下に移動します。私は自分の行を一様にしたいと思うし、すべて1ボタンの上に2つのボタンがあります。 – devander

+0

最初の応答は、RadioGroupを拡張する、参照されているMultiLineRadioGroupクラスを使用します。 – HomeIsWhereThePcIs

答えて

0

MultiLineRadioGroupクラス:

<TableLayout 
       android:id="@+id/tableLayout2" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:divider="?android:attr/dividerHorizontal" 
       android:showDividers="middle" 
       android:shrinkColumns="0" 
       > 
</TableLayout> 

これは基本的に私が見えるように私のtablerowsを取得しようとしていますものですちょうど複数の方法で動作しないと示唆されたので、私は描画ボードに戻った。私が見つけた解決策は、ラジオ・グループ全体を飛ばしただけでした。テーブル行のradiogroupではなく、3つのラジオボタンを含むRelativeLayoutを配置し、LayoutParamsを使用して、ボタンを希望の方法で配置し、幅/高さ/重さを設定しました。私はその後、onCheckedChangeListenerの代わりに各ラジオボタンに対して別々のonClickListenerを作成しました。だから私は自分のクラスと機能を書く必要がありましたが、価値があると判明しました。 マイカスタムクラス:

public class FancyRadioGroup{ 
    public FancyRadioGroup(Context context, TableRow tableRow, TextView 
textView, RadioButton pButton, RadioButton mButton, RadioButton rButton){ 
     TableRow.LayoutParams rowParams= new 
TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 
TableRow.LayoutParams.MATCH_PARENT, 1.0f); 
     textView.setLayoutParams(rowParams); 

     RelativeLayout relativeLayout = new RelativeLayout(context); 
     tableRow.addView(relativeLayout); 
     relativeLayout.setLayoutParams(rowParams); 

     relativeLayout.addView(pButton); 
     relativeLayout.addView(mButton); 

     RelativeLayout.LayoutParams topLParams= new 
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
RelativeLayout.LayoutParams.WRAP_CONTENT); 
     topLParams.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
     pButton.setLayoutParams(topLParams); 

     RelativeLayout.LayoutParams topRParams= new 
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
RelativeLayout.LayoutParams.WRAP_CONTENT); 
     topRParams.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
     topRParams.addRule(RelativeLayout.RIGHT_OF,pButton.getId()); 
     mButton.setLayoutParams(topRParams); 

     RelativeLayout.LayoutParams botParams= new 
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
RelativeLayout.LayoutParams.WRAP_CONTENT); 
     botParams.addRule(RelativeLayout.BELOW,mButton.getId()); 
     relativeLayout.addView(rButton); 
     rButton.setLayoutParams(botParams); 
    } 
} 

そして編集した既存のクラス:

public PMR(final TableLayout tableLayout, final TableRow tableRow, final 
TextView textView, final Context context, String caption){ 
    this.caption=caption; 
    textView.setTag("incomplete"); 
    repairString=null; 

    pRadioButton = new RadioButton(context); 
    pRadioButton.setText("Present"); 
    pRadioButton.setId(generateViewId()); 
    final int pId=pRadioButton.getId(); 

    mRadioButton = new RadioButton(context); 
    mRadioButton.setText("Missing"); 
    mRadioButton.setId(generateViewId()); 
    final int mId=mRadioButton.getId(); 

    rRadioButton = new RadioButton(context); 
    rRadioButton.setText("Repairs Needed"); 
    rRadioButton.setId(generateViewId()); 
    final int rId=rRadioButton.getId(); 

    final FancyRadioGroup fancyRadioGroup = new FancyRadioGroup(context, 
tableRow, textView, pRadioButton, mRadioButton, rRadioButton); 

    final LinearLayout textLayout=new LinearLayout(context); 
    textLayout.setOrientation(LinearLayout.HORIZONTAL); 
    tableLayout.addView(textLayout); 
    textLayout.setTag("Text Row"); 

    final TextView labelText = new TextView(context); 
    labelText.setText(""); 
    textLayout.addView(labelText); 

    repairEditText = new EditText(context); 
    repairEditText.setEnabled(false); 
    repairEditText.setText(""); 
    textLayout.addView(repairEditText); 

    final String prefix="Notes: "; 
    pRadioButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      mRadioButton.setChecked(false); 
      if (repairEditText.isEnabled()){ 
       repairEditText.setEnabled(false); 
       labelText.setText(""); 
      } 
      textView.setTag("Present"); 
      rRadioButton.setChecked(false); 
     } 
    }); 
    mRadioButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      pRadioButton.setChecked(false); 
      if (repairEditText.isEnabled()){ 
       repairEditText.setEnabled(false); 
       labelText.setText(""); 
      } 
      textView.setTag("Missing"); 
      rRadioButton.setChecked(false); 
     } 
    }); 
    rRadioButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      mRadioButton.setChecked(false); 
      pRadioButton.setChecked(false); 
      repairEditText.setEnabled(true); 
      repairEditText.requestFocus(); 
      labelText.setText(prefix); 
      textView.setTag("Repairs Needed"); 
     } 
    }); 
} 
関連する問題