Android的 - TextView中的权重属性,作为的LinearLayout ListView中排查看使用时被忽略 [英] Android - TextView's Weight Property in LinearLayout is being ignored when used as ListView row View

查看:388
本文介绍了Android的 - TextView中的权重属性,作为的LinearLayout ListView中排查看使用时被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CustomListView,当我longClick上一排,那行更改为不同的listRow布局的布局。到目前为止,一切正常,但是,新视图的布局为com pressed。这里是我的xml文件:

I have a CustomListView, when I longClick on a row, the layout of that row changes to a different listRow Layout. So far, everything is working, however, the layout of the new view is compressed. Here are my xml files:

search_result_layout.xml - >这是由customListView适配器使用原始的XML文件

search_result_layout.xml --> this is the original xml file used by the customListView Adapter.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/search_result_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="50dp"
    android:orientation="horizontal"
    android:padding="1dip" >

    <TextView
        android:id="@+id/dialogItemName"
        android:layout_width="0dp"
        android:layout_weight="0.54"
        android:layout_marginRight="5dp"
        android:layout_height="match_parent"
        android:text="Item Name"
        android:gravity="center"
        android:textSize="10pt" />


    <TextView
        android:id="@+id/price"
        android:layout_width="0dp"
        android:layout_weight="0.14"
        android:layout_height="match_parent"
        android:text="Price"
        android:gravity="center"
        android:textSize="10pt" />

    <TextView
        android:id="@+id/discount"
        android:layout_width="0dp"
        android:layout_weight="0.10"
        android:layout_height="match_parent"
        android:text="Discount"
        android:gravity="center"
        android:textSize="10pt" />

    <TextView
        android:id="@+id/qtyInput"
        android:layout_width="0dp"
        android:layout_weight="0.14"
        android:layout_height="match_parent"
        android:text="qtyInput"
        android:gravity="center"
        android:textSize="10pt" />

    <TextView
        android:id="@+id/discInput"
        android:layout_width="0dp"
        android:layout_weight="0.11"
        android:layout_height="match_parent"
        android:text="discInput"
        android:gravity="center"
        android:textSize="10pt" />


</LinearLayout>

下面是我的search_result_inflate.xml文件

Here is my search_result_inflate.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="50dp"
    android:orientation="horizontal"
    android:padding="1dip" >

    <TextView
        android:id="@+id/dialogItemName"
        android:layout_width="0dp"
        android:layout_weight="0.54"
        android:layout_marginRight="5dp"
        android:layout_height="match_parent"
        android:text="Item Name"
        android:gravity="center"
        android:textSize="10pt" />


    <TextView
        android:id="@+id/price"
        android:layout_width="0dp"
        android:layout_weight="0.14"
        android:layout_height="match_parent"
        android:text="Price"
        android:gravity="center"
        android:textSize="10pt" />

    <TextView
        android:id="@+id/discount"
        android:layout_width="0dp"
        android:layout_weight="0.10"
        android:layout_height="match_parent"
        android:text="Discount"
        android:gravity="center"
        android:textSize="10pt" />

    <EditText
        android:id="@+id/qtyInput"
        android:layout_width="0dp"
        android:layout_weight="0.14"
        android:layout_height="match_parent"
        android:hint="qtyInput"
        android:inputType="number"
        android:gravity="center"
        android:textSize="10pt" />

    <EditText
        android:id="@+id/discInput"
        android:layout_width="0dp"
        android:layout_weight="0.11"
        android:layout_height="match_parent"
        android:hint="discInput"
        android:inputType="number|date"
        android:gravity="center"
        android:textSize="10pt" />


</LinearLayout>

的两个文件是几乎相同,只有最后两个TextViews成为EditTexts代替,因此用户可以输入他或她想要的值。

The two files are almost identical, only that the last two TextViews become EditTexts instead so the user can input the values he or she wants.

此外,请注意,我放在重量在我的textViews和EditTexts,这样,当用户从纵向移动视图以景观空间最大化。

Also, kindly notice that I placed weight in my textViews and EditTexts, so that the spaces are maximized when the user shifts the view from portrait to landscape.

resultListView.setOnItemLongClickListener(new OnItemLongClickListener() {

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view,
            int position, long id) {
        // TODO Auto-generated method stub

        TextView textViewQuantity = (TextView)view.findViewById(R.id.qtyInput);
        TextView textViewDiscountReq = (TextView)view.findViewById(R.id.discInput);
        TextView textViewName = (TextView)view.findViewById(R.id.dialogItemName);
        TextView textViewPrice = (TextView)view.findViewById(R.id.price);
        TextView textViewDiscount = (TextView)view.findViewById(R.id.discount);

        textViewQuantity.setVisibility(View.GONE);
        textViewDiscountReq.setVisibility(View.GONE);
        textViewName.setVisibility(View.GONE);
        textViewPrice.setVisibility(View.GONE);
        textViewDiscount.setVisibility(View.GONE);

        LinearLayout ll_inflate = (LinearLayout)view.findViewById(R.id.search_result_layout);
        Log.d("Angelo", "original width = " + ll_inflate.getWidth());

        View child = getLayoutInflater().inflate(R.layout.search_result_inflate, null);

        TextView newText = (TextView)child.findViewById(R.id.qtyInput);
        newText.setText(textViewQuantity.getText().toString());

        EditText enter_txt = (EditText)child.findViewById(R.id.qtyInput);   

        ll_inflate.addView(child);
        Log.d("Angelo", "not original width = " + ll_inflate.getWidth());


        return false;
    }
});

当我longClick一个项目,布局的变化。然而,TextViews和EditTexts都是彼此相邻,仿佛重量属性被忽略了。起初我以为宽可能前后视图更改后已经改变,但日志告诉我,它有1136幅(我使用的是第二代的Nexus 7)。现在我的空白,母公司的宽度并没有改变,但权重值被忽略了。我该如何解决这个问题?将编程分配重量可行的解决方案?

When the I longClick an item, the layout changes. However, the TextViews and the EditTexts are all next to one another, as if the weight property was ignored. At first I thought that the width might have changed before and after the view was changed, but the Log told me it had 1136 width (I'm using a 2nd Gen Nexus 7). Now I'm blank, the width of the parent didn't change but the weight value was ignored. How do I fix this? Would assigning the weight programmatically a viable solution?

此外,跟进的问题,我只希望最后2 TextViews更改为EditTexts。所以我尽量做到及时达到这样的效果是,从原来的布局,我拉头3字段的值(项目名称,价格,折扣)做 textView.getText()。的toString(从新布局)并获得textviews并使用的setText()但到目前为止,它没有工作,有什么建​​议?

Also, follow up question, I only want the last 2 TextViews to change to EditTexts. So what I try to do to achieve that effect is that from the original layout, I pull the values of the first 3 fields (Item Name, Price, Discount) by doing textView.getText().toString() and get the textviews from the new layout and use setText() but so far, it did not work, any suggestions?

更新:

<一个href=\"http://stackoverflow.com/questions/16278159/why-linearlayouts-margin-is-being-ignored-if-used-as-listview-row-view/16278769#16278769\">Upon阅读本链接,我想,当我使用的LinearLayout作为我的ListView行观的LinearLayout内TextViews的Weight属性被忽略。嵌套另一个线性布局并没有帮助,所以尽管。

Upon reading this link, I think that the Weight property of the TextViews inside the LinearLayout is being ignored when I use the LinearLayout as my ListView Row View. Nesting another Linear Layout did not help much either.

我放在另一个里面的LinearLayout这样:

I placed another LinearLayout inside as such:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="50dp"
    android:orientation="horizontal"
    android:padding="1dip" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:padding="1dip" >

        <TextView
            android:id="@+id/dialogItemName"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginRight="5dp"
            android:layout_weight="0.70"
            android:gravity="center"
            android:text="Item Name"
            android:textSize="23sp" />

        <TextView
            android:id="@+id/price"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.16"
            android:gravity="center"
            android:text="Price"
            android:textSize="23sp" />

        <TextView
            android:id="@+id/discount"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.12"
            android:gravity="center"
            android:text="Discount"
            android:textSize="23sp" />

        <EditText
            android:id="@+id/qtyInput"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.16"
            android:gravity="center"
            android:hint="qtyInput"
            android:inputType="number"
            android:textSize="23sp" >

            <requestFocus />
        </EditText>

        <EditText
            android:id="@+id/discInput"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.13"
            android:gravity="center"
            android:hint="discInput"
            android:inputType="number|date"
            android:textSize="23sp" />
    </LinearLayout>

</LinearLayout>

任何想法?

推荐答案

呼叫来代替:

ll_inflate.addView(child, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

这篇关于Android的 - TextView中的权重属性,作为的LinearLayout ListView中排查看使用时被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆