2013-02-27 46 views
1

在XML中,我有一個自定義列表項目視圖,在適配器的getView()方法內爲我的ListView充氣。在XML中,如何讓我的自定義ListView項目總是屏幕寬度的一半(也許還會添加一些填充)?我不認爲添加其他小工具會像我見過的建議解決方案一樣工作,因爲這是一個列表項。將ListView項目設置爲XML中的一半ScreenWidth

編輯:

爲了澄清,我不認爲這是我的XML代碼的一個問題,但這裏是我的意思。如果我有一個自定義佈局是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/LinearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="1.0" 
    android:paddingTop="10dp" 
    > 

    <ImageView 
     android:id="@+id/item_image" 
     android:layout_width="218dp" 
     android:layout_height="212dp" 
     android:layout_gravity="right" 
     android:adjustViewBounds="true" 

     android:scaleType="fitCenter" 
     android:src="@drawable/..." /> 

</LinearLayout> 

我有一個硬編碼的寬度(我不認爲這是最好的做法),所以我要作出這樣的寬屏幕總寬度的1/2。顯然,這個XML非常簡單,但我希望它能解決這個問題。

道歉,如果我失去了一些相當明顯的東西。

編輯

添加具有相同的解決了這個另一種佈局。

編輯2: 沒關係。我注意到了以下行爲: 如果我添加另一個佈局,它將虛增所有內容,所以我會從字面上爲每個列表項獲取一半的空白寬度。我必須將最頂部的佈局設置爲我猜想的屏幕寬度的一半。我該如何解決這個問題?

+0

http://stackoverflow.com/questions/6094380/android-two-listview-vertically-using-50-50-of-the-screen-height – hakiko 2013-02-27 04:36:15

+0

可以添加xml代碼的客戶佈局的列表視圖 – 2013-02-27 04:36:17

+0

@hakiko我能夠在活動中的兩個ListViews之間有一個50/50寬度的關係,但是我將_to放在每個列表item_中的自定義佈局在兩個ListView中被切斷,因爲它的寬度超過屏幕的50%。這是我的問題。 – Kgrover 2013-02-27 04:38:14

回答

2

main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/LinearLayout1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
tools:context=".MainActivity" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="vertical" > 

     <ListView 
      android:id="@+id/listView1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 
     </ListView> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="vertical" > 
    </LinearLayout> 
</LinearLayout> 

itemlist.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="match_parent" 
android:orientation="vertical" > 

<LinearLayout 
    android:id="@+id/root" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#000000" 
    android:gravity="center" 
    android:orientation="horizontal" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 
</LinearLayout> 

MainActivity.java

public class MainActivity extends Activity { 

static final String TAG_NAME = "name"; 
static final String TAG_ID = "id"; 
static String selected = ""; 

ListView listView; 


HashMap<String, String> map; 
ArrayList<HashMap<String,String>> myList; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    myList = new ArrayList<HashMap<String,String>>();  
    listView = (ListView) findViewById(R.id.listView1); 
    fillListView(); 
} 

private void fillListView() { 

    myList.clear(); 

    map = new HashMap<String, String>(); 
    map.put(TAG_ID,"1"); 
    map.put(TAG_NAME, "Name1"); 
    myList.add(map); 

    map = new HashMap<String, String>(); 
    map.put(TAG_ID,"2"); 
    map.put(TAG_NAME, "Name2"); 
    myList.add(map); 

    map = new HashMap<String, String>(); 
    map.put(TAG_ID,"3"); 
    map.put(TAG_NAME, "Name3"); 
    myList.add(map); 

    map = new HashMap<String, String>(); 
    map.put(TAG_ID,"4"); 
    map.put(TAG_NAME, "Name4"); 
    myList.add(map); 

    listView.setAdapter(new MyListAdapter(this, R.layout.itemlist, myList)); 

} 
private class MyListAdapter extends BaseAdapter 
{ 

    private List<HashMap<String,String>> List; 
    private LayoutInflater mInflater; 
    private int layoutresource; 

    public MyListAdapter(Context context,int resource,List<HashMap<String, String>> myList) { 
     List = myList; 
     mInflater = LayoutInflater.from(context); 
     layoutresource = resource; 
    } 

    @Override 
    public int getCount() { 
     return List.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return List.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 

     if (convertView == null) { 
      convertView = mInflater.inflate(layoutresource,null); 
     }  

     final View rootView = convertView.findViewById(R.id.root); 

     ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1); 
     final TextView name = (TextView) convertView.findViewById(R.id.textView1); 

     imageView.setImageDrawable(getResources().getDrawable(R.drawable.profile)); 
     name.setTextColor(Color.parseColor("#ffffff")); 
     name.setText(List.get(position).get(TAG_NAME)); 

     if (List.get(position).get(TAG_ID).equals(selected)) 
      name.setTextColor(Color.parseColor("#ff0000")); 
     else 
      name.setTextColor(Color.parseColor("#ffffff")); 


     rootView.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       selected = List.get(position).get(TAG_ID); 
       fillListView(); 
      } 
     }); 

     name.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       rootView.performClick(); 
      } 
     }); 

     imageView.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       rootView.performClick(); 
      } 
     }); 

     return convertView; 
    } 
} 

}

+0

我的問題不在主要活動中。它正在設置自定義佈局_list item_的寬度。 – Kgrover 2013-02-27 04:47:15

+0

請檢查更新的答案 – 2013-02-27 04:50:47

+0

是的,我只是想到了這一點。那麼,我會給你答案的答案。謝謝。 – Kgrover 2013-02-27 04:54:16

0

裏面有隻有一個視圖(ImageView的),你listitem.so你應該列表視圖,而不是名單的一半寬度item.but如果你真的想列表項的寬度的一半然後給下面的代碼一試,我希望這會有所幫助。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/LinearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:weightSum="10" 
    android:gravity="center" 
    android:paddingTop="10dp" 
    > 

    <ImageView 
     android:id="@+id/item_image" 
     android:layout_width="0dp" 
     android:layout_weight="5" 
     android:layout_height="212dp" 
     android:adjustViewBounds="true" 

     android:scaleType="fitCenter" 
     android:src="@drawable/..." /> 

</LinearLayout> 
+0

準確使用屏幕的50%寬度的listview我想做的就這樣。當我這樣做時,我無法讓它移動到中心。任何建議? – Kgrover 2013-02-27 05:16:58

+0

是的,只需設置父view.check我的更新答案的嚴重性。 – 2013-02-27 05:26:20

+0

@Kgrover:它工作與否? – 2013-02-27 07:08:12

1

簡化

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/LinearLayout1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="horizontal" 
      android:paddingTop="10dp"> 
<ImageView 
     android:id="@+id/item_image" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="212dp" 
     android:layout_gravity="right" 
     android:adjustViewBounds="true" 
     android:scaleType="fitCenter" 
     android:background="#FFFFFF" 
     android:src="@drawable/ic_launcher" /> 
<View android:layout_height="0dp" 
     android:layout_width="0dp" 
     android:layout_weight="1"/> 
</LinearLayout> 
相關問題