2015-08-09 28 views
0
2個TextView的對話框的列表視圖側

我想實現:的Android與側

TextView1 TextView2

卻是露出由行

ListView.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:layout_marginTop="5dp" 
    > 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

    </ListView> 

</RelativeLayout> 

行另一個用於定製adatpter的xml

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

> 

<TextView 
    android:id="@+id/dialog_block_territory_name" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_weight="0" 

    /> 

<TextView 
    android:id="@+id/dialog_block_territory_id" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_toRightOf="@+id/dialog_block_territory_name" 
    android:layout_weight="0" 
    /> 


    </RelativeLayout> 

定製適配器代碼:

public class TerritorySpinnerDisplayAdapter extends BaseAdapter { 

private Context mContext; 
private ArrayList<String> territoryName; 
private ArrayList<String> territoryId ; 

public TerritorySpinnerDisplayAdapter(Context c,ArrayList<String> territoryName,ArrayList<String> territoryId) { 
    // TODO Auto-generated constructor stub 

    this.mContext = c; 
    this.territoryName = territoryName; 
    this.territoryId = territoryId; 

} 


@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return territoryName.size(); 
} 

@Override 
public Object getItem(int arg0) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public long getItemId(int arg0) { 
    // TODO Auto-generated method stub 
    return 0; 
} 

@Override 
public View getView(int pos, View child, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    Holder mHolder; 
    LayoutInflater layoutInflater; 
    if (child == null) { 
     mHolder = new Holder(); 
     layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     child = layoutInflater.inflate(R.layout.list_territory_dialog_block,null); 

     mHolder.territoryNameView = (TextView) child.findViewById(R.id.dialog_block_territory_name); 
     mHolder.territoryIdView = (TextView) child.findViewById(R.id.dialog_block_territory_id); 

     child.setTag(mHolder); 
    }else { 
     mHolder = (Holder) child.getTag(); 
    } 
    mHolder = (Holder) child.getTag(); 

    if(pos < territoryName.size() && mHolder.territoryNameView!=null){ 
     String name = territoryName.get(pos); 
     mHolder.territoryNameView.setText(name); 

    } 
    if(pos < territoryId.size() && mHolder.territoryIdView!=null){ 
    String bid = territoryId.get(pos); 
    mHolder.territoryIdView.setText(bid); 

    } 


    //Log.d("@@@@@@@@@@@@@@@@@@@@@@@@[email protected]@@@@@@@@@@@@@@@@@@@",blockName.get(0) + blockId.get(0) + territoryId.get(0)); 
    return child; 
} 

public class Holder { 
    TextView territoryNameView; 
    TextView territoryIdView; 


} 

} 

final Dialog dialog = new Dialog(getActivity()); 
      dialog.setContentView(R.layout.list_view); 
      dialog.setTitle("Select City"); 
      ListView listView = (ListView) dialog.findViewById(R.id.list); 

      dialog.show(); 
territoryAdapter = newTerritorySpinnerDisplayAdapter(getActivity(), 
      arrTerr, arrTerr1); 
      listView.setAdapter(territoryAdapter); 
      territoryAdapter.notifyDataSetChanged(); 

我Tablelayout試過了,還是結果相同

我想,這可能是XML文件的問題,

回答

1

嘗試這樣做...

  1. 使用的LinearLayout,而不是RelativeLayout的
  2. 使用Android:方向= 「橫向」 中的LinearLayout
  3. 設置兩個文本框中的android:layout_width = 「0dp」
  4. 設置兩個文本框中的android:layout_weight = 「1」

這應該達到你要找的。

+0

運行良好,添加數組和佈局時出現問題,感謝您的幫助! – user3140171