У меня есть Галерея, отображающая TextViews. Когда он отображается, 1-й элемент не отображается. Это потому, что parent.getWidth () возвращает 0 при первом вызове.
Так, как я могу установить ширину элемента, чтобы быть частью ширины его родительской галереи?
Вот код:
public class GalleryActivity extends Activity {
private Gallery gallery;
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.gallery);
gallery = (Gallery) findViewById (R.id.gallery);
gallery.setAdapter (new Adapter ());
}
private class Adapter extends BaseAdapter {
private String[] items = {"A", "B", "C", "D", "E", "F"};
public int getCount () {
return items.length;
}
public Object getItem (int position) {
return items[position];
}
public long getItemId (int position) {
return position;
}
public View getView (int position, View convertView, ViewGroup parent) {
TextView view = new TextView (GalleryActivity.this);
view.setText (getItem (position).toString ());
view.setBackgroundColor (Color.GRAY);
view.setGravity (Gravity.CENTER);
view.setLayoutParams (new Gallery.LayoutParams (parent.getWidth () / 3,
LayoutParams.FILL_PARENT));
return view;
}
}
}
макет / gallery.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Gallery android:id="@+id/gallery" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"
android:scrollbars="horizontal" android:spacing="1dp"></Gallery>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="3"></TextView>
</LinearLayout>