0

下面是一个 list_item 布局。我不明白为什么列表项行内的 viewtext 元素和分隔元素之间有很大的垂直空间?这种布局有什么问题?

截图在这里

我的列表项的布局 xml:

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

    <TextView
            android:id="@+id/companyName"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/table_text_selector"/>

    <TextView
            android:id="@+id/exchange"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/table_text_light_selector"
            android:layout_below="@id/companyName"/>

    <TextView
            android:id="@+id/symbol"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/table_text_light_selector"
            android:layout_below="@id/companyName"
            android:layout_toLeftOf="@id/exchange"/>

    <TextView
            android:id="@+id/price"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:singleLine="true"
            android:ellipsize="end"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/table_text_selector"
            android:layout_alignParentRight="true"/>

    <TextView
            android:id="@+id/change"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/table_stock_change_text"
            android:layout_alignParentRight="true"
            android:layout_below="@id/price"/>

    <TextView
            android:id="@+id/percentChange"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/table_stock_change_text"
            android:layout_alignBaseline="@id/change"
            android:layout_alignBottom="@id/change"
            android:layout_toLeftOf="@id/change"
            android:layout_marginRight="5dp"/>

</RelativeLayout>

这是片段:

public class StockListFragment extends ItemListFragment<CurrentStock> {

    @Inject BootstrapServiceProvider serviceProvider;
    @Inject LogoutService logoutService;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Injector.inject(this);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        setEmptyText(R.string.no_stocks);


    }

    @Override
    protected void configureList(Activity activity, ListView listView) {
        super.configureList(activity, listView);

        listView.setFastScrollEnabled(true);
        listView.setDividerHeight(0);

        getListAdapter().addHeader(activity.getLayoutInflater()
                        .inflate(R.layout.stock_list_item_labels, null));
    }

    @Override
    LogoutService getLogoutService() {
        return logoutService;
    }


    @Override
    public Loader<List<CurrentStock>> onCreateLoader(int id, Bundle args) {
        final List<CurrentStock> initialItems = items;
        return new ThrowableLoader<List<CurrentStock>>(getActivity(), items) {
            @Override
            public List<CurrentStock> loadData() throws Exception {

                try {
                    List<CurrentStock> latest = null;

                    if(getActivity() != null)
                        latest = serviceProvider.getService(getActivity()).getStocks();

                    if (latest != null)
                        return latest;
                    else
                        return Collections.emptyList();
                } catch (OperationCanceledException e) {
                    Activity activity = getActivity();
                    if (activity != null)
                        activity.finish();
                    return initialItems;
                }
            }
        };

    }

    public void onListItemClick(ListView l, View v, int position, long id) {
        CurrentStock currentStock = ((CurrentStock) l.getItemAtPosition(position));

        startActivity(new Intent(getActivity(), StockActivity.class).putExtra(STOCK, currentStock));
    }

    @Override
    public void onLoadFinished(Loader<List<CurrentStock>> loader, List<CurrentStock> items) {
        super.onLoadFinished(loader, items);

    }

    @Override
    protected int getErrorMessage(Exception exception) {
        return R.string.error_loading_stocks;
    }

    @Override
    protected SingleTypeAdapter<CurrentStock> createAdapter(List<CurrentStock> items) {
        return new StockListAdapter(getActivity().getLayoutInflater(), items);
    }
}

这是适配器

public class StockListAdapter extends SingleTypeAdapter<CurrentStock> {

    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MMMM dd");

    /**
     * Create adapter
     *
     * @param inflater
     * @param items
     */
    public StockListAdapter(LayoutInflater inflater, List<CurrentStock> items) {
        super(inflater, R.layout.stock_list_item);
        setItems(items);
    }

    /**
     * Create adapter
     *
     * @param inflater
     */
    public StockListAdapter(LayoutInflater inflater) {
        this(inflater, null);

    }

    @Override
    public long getItemId(final int position) {
        final String id = getItem(position).getSymbol();
        return !TextUtils.isEmpty(id) ? id.hashCode() : super
                .getItemId(position);
    }
/**/
    @Override
    protected int[] getChildViewIds() {
        return new int[] { R.id.companyName, R.id.exchange, R.id.symbol, R.id.price, R.id.change, R.id.percentChange };
    }

    @Override
    protected void update(int position, CurrentStock currentStock) {
        setText(0, currentStock.getCompanyName());
        setText(1, currentStock.getExchange());
        setText(2, currentStock.getSymbol());
        setText(3, currentStock.getPrice().toString());
        setText(4, currentStock.getChange().toString());
        setText(5, currentStock.getPercentChange());
    }


}

我没有它自己的列表视图。顺便提一句。除了顶部和底部分隔器之外,我需要一个分隔器吗?

4

3 回答 3

0
// try this
<?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:padding="5dp">

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

        <TextView
            android:id="@+id/companyName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textColor="@color/table_text_light_selector"
            android:text="companyName"
            android:textAppearance="?android:attr/textAppearanceMedium"/>

        <LinearLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent">
            <TextView
                android:id="@+id/symbol"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:textColor="@color/table_text_light_selector"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="symbol"/>
            <TextView
                android:id="@+id/exchange"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="exchange"
                android:layout_marginTop="5dp"
                android:layout_marginLeft="5dp"
                android:textColor="@color/table_text_light_selector"
                android:textAppearance="?android:attr/textAppearanceSmall"/>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:gravity="right"
        android:orientation="vertical">

        <TextView
            android:id="@+id/price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:ellipsize="end"
            android:textColor="@color/table_text_light_selector"
            android:text="price"
            android:textAppearance="?android:attr/textAppearanceMedium"/>

        <LinearLayout
            android:layout_height="wrap_content"
            android:layout_width="wrap_content">
            <TextView
                android:id="@+id/percentChange"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="percentChange"
                android:layout_marginTop="5dp"
                android:textColor="@color/table_text_light_selector"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:layout_marginRight="5dp"/>
            <TextView
                android:id="@+id/change"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="change"
                android:layout_marginTop="5dp"
                android:textColor="@color/table_text_light_selector"
                android:textAppearance="?android:attr/textAppearanceSmall"/>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>
于 2013-10-25T11:40:22.003 回答
0

试试下面的代码。它会给你你需要的类似结果:

<?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
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center"
        android:gravity="center">

    <TextView
            android:id="@+id/companyName"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:singleLine="true"
            android:text="facebook"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:textColor="@color/table_text_selector"
            android:textAppearance="?android:attr/textAppearanceMedium"/>

    <TextView
            android:id="@+id/price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="facebook"
            android:layout_marginRight="5dp"
            android:textColor="@color/table_text_selector"
            android:textAppearance="?android:attr/textAppearanceMedium"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_marginTop="5dp">

    <TextView
            android:id="@+id/exchange"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="facebook"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:textColor="@color/table_text_light_selector"
            android:textAppearance="?android:attr/textAppearanceSmall"/>

    <TextView
            android:id="@+id/percentChange"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="22%"
            android:textColor="@color/table_stock_change_text"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:layout_marginRight="5dp"/>

    <TextView
            android:id="@+id/change"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0.0"
            android:layout_marginRight="5dp"
            android:textColor="@color/table_stock_change_text"
            android:textAppearance="?android:attr/textAppearanceSmall"/>

    </LinearLayout>

</LinearLayout>
于 2013-10-25T11:40:40.783 回答
0

问题在于您使用分隔符代码的代码。顺便说一句,你为什么要这样做?...因为 ListView 有它的dividerand dividerHeight?

这是我在几分钟内使用上述布局进行编码后得出的结果(仅删除了textColor属性): 在此处输入图像描述

上面列表的代码:

public class MyListActivity extends FragmentActivity {
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstance) {
        super.onCreate(savedInstance);
        setContentView(R.layout.so_list_layout);
        listView = (ListView) findViewById(R.id.localListView);
        fillListView();
    }

    private void fillListView() {
        final List<LDataRow> data = getRowData();
        ArrayAdapter<LDataRow> adapter = new ArrayAdapter<MyListActivity.LDataRow>(this,
                R.layout.so_list_row, data) {
            private LDataRow getDataRow(int position) {
                return data.get(position);
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                LDataRow row = getDataRow(position);
                if (convertView == null) {
                    convertView = LayoutInflater.from(MyListActivity.this).inflate(
                            R.layout.so_list_row, parent, false);
                }
                TextView companyName = (TextView) convertView.findViewById(R.id.companyName);
                companyName.setText(row.getCompanyName());
                TextView exchange = (TextView) convertView.findViewById(R.id.exchange);
                exchange.setText(row.getExchange());
                TextView symbol = (TextView) convertView.findViewById(R.id.symbol);
                symbol.setText(row.getSymbol());
                TextView price = (TextView) convertView.findViewById(R.id.price);
                price.setText(String.valueOf(row.getPrice()));
                TextView change = (TextView) convertView.findViewById(R.id.change);
                change.setText(String.valueOf(row.getChange()));
                TextView percentChange = (TextView) convertView.findViewById(R.id.percentChange);
                percentChange.setText(String.valueOf(row.getPercentChange()));
                return convertView;
            }
        };
        listView.setAdapter(adapter);
    }

    private List<LDataRow> getRowData() {
        ArrayList<LDataRow> result = new ArrayList<MyListActivity.LDataRow>();
        result.add(new LDataRow("Facebook", "Nasdaq", "E", 52.445, 0.0, 0));
        result.add(new LDataRow("Advanced Micro", "NYSE", "D", 3.23, 0.0, 0));
        result.add(new LDataRow("Genmas", "CPH", "G", -100, -3.0, -20.0));
        return result;
    }

    private static class LDataRow {
        private String companyName;

        private String exchange;

        private String symbol;

        private double price;

        private double change;

        private double percentChange;

        public LDataRow(String companyName, String exchange, String symbol, double price,
                double change, double percentChange) {
            this.companyName = companyName;
            this.exchange = exchange;
            this.symbol = symbol;
            this.price = price;
            this.change = change;
            this.percentChange = percentChange;
        }

        public String getCompanyName() {
            return companyName;
        }

        public String getExchange() {
            return exchange;
        }

        public String getSymbol() {
            return symbol;
        }

        public double getPrice() {
            return price;
        }

        public double getChange() {
            return change;
        }

        public double getPercentChange() {
            return percentChange;
        }

    }
}

在顶部使用它RelativeLayout以获得底部间距:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="8dp" >

编辑如果要自定义分隔线,可以很好地指定自定义可绘制对象和大小。这是我用一个简单的layer-list,所做的my_so_divider.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape android:shape="rectangle" >
            <solid android:color="#ffffff" />
        </shape>
    </item>
    <item>
        <shape android:shape="line" >
            <stroke
                android:width="1dp"
                android:color="#000000" />
        </shape>
    </item>

</layer-list>

在 ListView 中设置:

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/localListView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ffff"
    android:divider="@drawable/my_so_divider"
    android:dividerHeight="24dp"
    android:footerDividersEnabled="false"
    android:headerDividersEnabled="false" >

</ListView>

检查footerDividersEnabledheaderDividersEnabled和属性divider...dividerHeight随意使用drawable。下图效果: 在此处输入图像描述

于 2013-10-25T11:46:35.290 回答