如何在LinearLayout中的片段中设置textview的layout_weight

时间:2016-04-14 05:00:47

标签: android android-layout android-fragments android-layout-weight

我正在片段中的ListView + Checkbox上创建一个程序,但是演示文稿正在创建问题...... 实际上我已经设置了textview和checkbox的layout_weight,但是它不起作用,所以我想以编程方式设置它,而我所引用的复选框+ textview不在当前布局中,实际上我在当前布局中对它进行充气。我没有找到任何前面的例子,在一个片段中实现...请看看我的代码,请帮助我

这是我的代码:

fragment_packages.XML:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/activity_horizontal_margin"
android:orientation="vertical">
<ListView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:paddingBottom="30dp"
android:id="@+id/packages_list"/>
<LinearLayout
android:layout_weight="0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start|end"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:paddingBottom="8dp"
>
<TextView
android:id="@+id/tv_total"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="9"
android:text="@string/totalcost"
android:textSize="20sp"/>
<TextView
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/price"
android:textSize="20sp"
android:layout_gravity="end"
/>
</LinearLayout>
</LinearLayout>

packages_items.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:layout_alignParentTop="true"
    android:orientation="horizontal"
    android:id="@+id/linearlayout1"
    >

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:id="@+id/checkbox"
    android:layout_weight="6"
        />
<TextView
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/items"
    android:textColor="@color/colorPrimary"
    android:textSize="25sp"
    />
</LinearLayout>

片段:

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;


public class PackagesFragment extends Fragment
{
MyCustomAdpater adpater=null;
ArrayList<PackagesItems> packagesList=null;
ListView listView;
TextView priceView;
private float totalPrice=0f;

private class ViewHolder
{
    TextView textView;
    CheckBox checkBox;
}



@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    final ViewHolder holder=new ViewHolder();
    View rootView;


    rootView = inflater.inflate(R.layout.fragment_packages,container,false);
    listView=(ListView)rootView.findViewById(R.id.packages_list);
    priceView=(TextView)rootView.findViewById(R.id.price);


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

           PackagesItems packagesItems = (PackagesItems) parent.getItemAtPosition(position);

holder.checkBox = (CheckBox)view.findViewById(R.id.checkbox);

if(holder.checkBox.isChecked())
{
holder.checkBox.setChecked(false);
packagesItems.setSelected(!holder.checkBox.isChecked());
                subtractPrice(packagesList.get(position));
            }
            else {
                holder.checkBox.setChecked(true);
                packagesItems.setSelected(holder.checkBox.isChecked());
                addPrice(packagesList.get(position));
            }
            }
    });
    displayListView();

    return rootView;
}



public void displayListView()
{

    packagesList = new ArrayList<PackagesItems>();
    PackagesItems packagesItems = new PackagesItems("Hair Cut",150.00f,false);
    packagesList.add(packagesItems);
    packagesItems = new PackagesItems("Hair Spa",300.00f,false);
    packagesList.add(packagesItems);
    packagesItems = new PackagesItems("Facial",300.00f,false);
    packagesList.add(packagesItems);
    packagesItems = new PackagesItems("Make Up",300.00f,false);
    packagesList.add(packagesItems);

    adpater = new MyCustomAdpater(getActivity(),R.layout.packages_items,packagesList);

    listView.setAdapter(adpater);

}

private class MyCustomAdpater extends ArrayAdapter<PackagesItems>
{

    private ArrayList<PackagesItems> packagesList;

    public MyCustomAdpater(Context context, int textViewResourceId, ArrayList<PackagesItems> packagesList)
    {

        super(context,textViewResourceId,packagesList);

        this.packagesList = packagesList;


    }




    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;

        if(convertView==null)
        {
            LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.packages_items,null);
            holder = new ViewHolder();
            holder.textView = (TextView)convertView.findViewById(R.id.items);
            holder.checkBox=(CheckBox)convertView.findViewById(R.id.checkbox);
            holder.checkBox.setTextSize(25);
            holder.checkBox.setTextColor(getActivity().getResources().getColor(R.color.colorPrimary));

            convertView.setTag(holder);


            holder.checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    CheckBox cb= (CheckBox)v;
                    PackagesItems packagesItems = (PackagesItems)cb.getTag();
                    ;
                    if(cb.isChecked())
                    {
                        addPrice(packagesList.get(position));
                        packagesItems.setSelected(cb.isChecked());
                    }
                    else
                    {
                        subtractPrice(packagesList.get(position));
                        packagesItems.setSelected(!cb.isChecked());
                    }

                }
            });
        }
        else
        {
            holder=(ViewHolder)convertView.getTag();
        }
        PackagesItems packagesItems = packagesList.get(position);
        holder.textView.setText(String.valueOf(packagesItems.getPrice()));
        holder.checkBox.setText(packagesItems.getItem());
        holder.checkBox.setChecked(packagesItems.isSelected());
        holder.checkBox.setTag(packagesItems);
        return convertView;
    }


}

public void addPrice(PackagesItems packagesItems)
{float currentItemPrice;
    currentItemPrice= packagesItems.getPrice();

    totalPrice=totalPrice+currentItemPrice;

    priceView.setText(String.valueOf(totalPrice));
}
public void subtractPrice(PackagesItems packagesItems)
{float currentItemPrice;
    currentItemPrice= packagesItems.getPrice();

    totalPrice=totalPrice-currentItemPrice;

    priceView.setText(String.valueOf(totalPrice));
}

public void setView()
{

    //I WANT TO DO THE STUFF HERE, IN THIS METHOD - setView() !!!
}

}

3 个答案:

答案 0 :(得分:1)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:weightSum="5"
    android:gravity="center"
    android:orientation="vertical"
    android:layout_height="match_parent">


    <TextView
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:text="Hello"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:text="Hello"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:text="Hello"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:text="Hello"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="0dp"
        android:text="Hello"/>

</LinearLayout>

答案 1 :(得分:0)

使用

更改packages_items.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:weightSum="10"
    android:layout_alignParentTop="true"
    android:orientation="horizontal"
    android:id="@+id/linearlayout1"
    >

<CheckBox
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:id="@+id/checkbox"
    android:layout_weight="2"
        />
<TextView
    android:layout_weight="8"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/items"
    android:textColor="@color/colorPrimary"
    android:textSize="25sp"
    />
</LinearLayout>

在这里您可以找到差异,在复选框中定义宽度换行内容但是当使用布局权重时我们必须定义其中一个高度/宽度0dp

根据我们上一次评论,你可以做一件事改变我们的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:layout_alignParentTop="true"
        android:orientation="horizontal"
        android:id="@+id/linearlayout1"
        >

  <CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/checkbox"

        />
    </LinearLayout>

并在您的adpater中获取复选框的引用并使用checkbox..setText("your text");我希望它可以正常工作,如果它不这样,请以图像的形式发布您正在寻找的视图

答案 2 :(得分:0)

  

android:weight如何在没有android:weightSum的情况下工作?

android:weightSum的工作原理示例:

你有一个水平方向的LinearLayout,你里面有3个ImageViews,你希望这些ImageViews总是占用相同的空间。

    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:weightSum="3"
    android:orientation="horizontal"
    android:layout_gravity="center">

您可以将每个layout_weight的{​​{1}}设置为 1 ,将ImageView中的weightSum设置为 3 实现这一目标。

LinearLayout

好处是,这对于任何设备都能正常工作,如果直接设置宽度和高度,就不会发生这种情况。

Source