2016-08-30 3 views
0

EditTextとListViewを使用してToDoリストを作成しようとしています。テキストのフォント、色、サイズを変更するにはどうすればよいですか?配列アダプターを使用していくつかの回答を見ましたが、動的に作成されたListView項目にそれらを適用する方法はわかりません。ここで動的に作成されたListViewでテキストの色、サイズ、フォントを変更する方法

は、私がこれまで持っているものです。

ActivityMain.xml

<RelativeLayout 
    android:id="@+id/AgendaRL" 
    android:orientation="vertical" 
    android:background="#3E2723" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <TextView 
     android:id="@+id/agenda" 
     android:layout_width="370sp" 
     android:layout_height="wrap_content" 
     android:text="@string/agenda" 
     android:textSize="40sp" 
     android:textColor="#b7950b" 
     android:layout_marginTop="12sp" 
     android:layout_marginLeft="12sp" 
     android:layout_marginStart="12sp" 
     android:layout_marginBottom="0sp" /> 

    <View 
     android:background="#b7950b" 
     android:layout_below="@+id/agenda" 
     android:layout_width="28sp" 
     android:layout_height="36sp"/> 

    <EditText 
     android:id="@+id/aTask" 
     android:layout_below="@+id/agenda" 
     android:background="@drawable/ribbon" 
     android:inputType="text" 
     android:text="@string/Add_Task" 
     android:textColor="#3E2723" 
     android:maxLength="22" 
     android:maxLines="1" 
     android:layout_width="330sp" 
     android:layout_height="36sp" 
     android:textSize="28sp" 
     android:layout_marginLeft="28sp" 
     android:layout_marginStart="28sp"/> 

    <Button 
     android:id="@+id/Done" 
     style="?android:attr/borderlessButtonStyle" 
     android:layout_marginLeft="250sp" 
     android:layout_marginStart="250sp" 
     android:background="#b7950b" 
     android:text="@string/Done" 
     android:textColor="#3E2723" 
     android:textSize="18sp" 
     android:layout_below="@+id/agenda" 
     android:layout_width="48sp" 
     android:layout_height="36sp" 
     android:onClick="DoneClick"/> 

    <ListView 
     android:id="@+id/LVAgenda" 
     android:layout_below="@+id/aTask" 
     android:divider="@android:color/transparent" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

</RelativeLayout> 

MainActivity.Java

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
     ListView LVAgenda = (ListView) findViewById(R.id.LVAgenda); 
    arrayListAgenda = new ArrayList<String>(); 
    arrayAdapterAgenda = new ArrayAdapter<String>(this,  android.R.layout.simple_list_item_1, arrayListAgenda); 
    LVAgenda.setAdapter(arrayAdapterAgenda); 
} 

public void DoneClick(View v){ 
    EditText aTask = (EditText)findViewById(R.id.aTask); 
    String agenda = aTask.getText().toString().trim(); 

    if(agenda.isEmpty()){ 
     return; 
    } 

    arrayAdapterAgenda.add(agenda); 
    aTask.setText("Add task"); 


} 
+0

'ArrayAdapter'のカスタムサブクラスを作成し、' getView() 'をオーバーライドし、必要に応じてウィジェットを更新します。 – CommonsWare

+0

私はアンドロイドを使い始めていますが、おそらくコードを全長で表示できますか? – LunarLlama

+0

https://github.com/commonsguy/cw-omnibus/tree/master/Selection/Dynamicは、カスタムの「ArrayAdapter」を示しています。 – CommonsWare

答えて

1

commonswareは、あなたがこれを行うにはArrayAdapterのgetViewメソッドを()を使用することができます言ったように。

私はFacebookAdderセレクターをListAdapterで実装しました。私はコードを共有します。それが助けになるかもしれない。してみてください。

最初に、「行うべきリスト」の各項目のレイアウトを定義するレイアウトでXMLファイルを作成します。 私の場合、それはFacebookのプロフィール画像とチェックされたテキストボックスです。 (アライメント用のスペーサーもあります)

Facebook.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <ImageView 
      android:id="@+id/img" 
      android:layout_width="50dp" 
      android:layout_height="50dp" 
      android:paddingLeft="10dp"/> 

     <CheckedTextView 
      android:id="@+id/name" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center_vertical|right" 
      android:paddingLeft="10dp" 
      android:paddingRight="10dp" 
      android:checkMark="?android:attr/listChoiceIndicatorMultiple" 
      android:textColor="@android:color/black" 
      android:textStyle="bold" /> 

    </LinearLayout> 
    <View 
     android:id="@+id/spacer" 
     android:layout_width="match_parent" 
     android:layout_height="1dp" 
     android:background="@android:color/white"/> 
</LinearLayout> 

今すぐあなたのデータ配列を準備します。私の場合はカスタムクラスの配列で、各要素にはfacebookの名前、profilepic、およびbooleanが含まれています。

public class Item{ 
     public final String text; 
     public final Drawable icon; 
     public boolean isChecked; 
     public Item(String text, Drawable icon, boolean ischeck) { 
      this.text = text; 
      this.icon = icon; 
      this.isChecked = ischeck; 
     } 
     @Override 
     public String toString() { 
      return text; 
     } 
    } 

Iは、別のアクティビティからfriendsArrayを通過させることによって以下のコードを呼び出します。

データ配列が準備されました。これをListAdapter(私の場合はitems)に渡すことができます。

List Adapterの動作を理解してうれしいです。スクロール可能なリストを作成しました。このロジックがすることは、スクロール中にビューを再利用することです。

ListAdapter adapter = new ArrayAdapter<Item>(
       getActivity(), 
       android.R.layout.select_dialog_item, 
       android.R.id.text1, 
       items){ 
      public View getView(int position, View convertView, ViewGroup parent) { 
       View v = convertView; 
       FaceBookHolder fb = new FaceBookHolder(); 
       if(convertView == null) 
       { 
        LayoutInflater inflater = (LayoutInflater) getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        v = inflater.inflate(R.layout.facebook,null); 
        fb.Name = (CheckedTextView) v.findViewById(R.id.name); 
        fb.img = (ImageView) v.findViewById(R.id.img); 
        fb.spacer = (View) v.findViewById(R.id.spacer); 
        fb.Name.setOnClickListener(new View.OnClickListener() 
        { 
         @Override 
         public void onClick(View v) 
         { 
          CheckedTextView cv = (CheckedTextView)v; 
          if(cv.isChecked()) 
           cv.setChecked(false); 
          else 
           cv.setChecked(true); 
          for(int i =0;i< items.length; i++) 
          { 
           if(items[i].text == cv.getText()) 
           { 
            items[i].isChecked = cv.isChecked(); 
           } 
          } 
         } 
        }); 
        v.setTag(fb); 
       } 
       else 
        fb = (FaceBookHolder) v.getTag(); 
       Item itm = items[position]; 
       fb.Name.setText(itm.text); 
       fb.img.setImageDrawable(itm.icon); 
       fb.Name.setChecked(itm.isChecked); 
       return v; 
      } 
     }; 

は、あなたはしかし、それを変更することができるようにしたい、getViewメソッド()内のビューを取得します。(変更の色、フォントなど)

はそれが役に立てば幸い!乾杯!

+0

詳細な対応をありがとうございます!しかし、私はまだこれを作るのに問題があります。私はアンドロイドとプログラミング全般から始めているので、自分が持っているものを自分の状況に実際に適用することはできません。これまでのコードを追加します。 – LunarLlama

関連する問題