Android - TextView's Weight Property in LinearLayout is being ignored when used as ListView row View

这一生的挚爱 提交于 2020-01-16 10:00:20

问题


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 --> 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>

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>

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.

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;
    }
});

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?

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?

UPDATE:

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.

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>

Any ideas?


回答1:


Call instead:

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


来源:https://stackoverflow.com/questions/25159494/android-textviews-weight-property-in-linearlayout-is-being-ignored-when-used

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!