动态调用后如何调整片段的权重?

时间:2015-10-09 05:00:58

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

我的活动有两个片段。一个是列表,另一个是详细信息。每个的重量分别为1和3。现在我要做的是当用户点击其中一个列表对象时,另一个片段将从屏幕右侧弹出,占据屏幕的1/5左右,所以基本上权重应该是1,3和1但是发生的事情是,第三个片段占据了屏幕的近95%。

这是我的代码:

public class BracketActivity extends TorneyoBaseActivity implements Fragment2FragmentCommunicator {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bracket);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_bracket, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void loadItemToDetailFragment(Object item) {
        if(item instanceof Bracket){
            Bracket data = (Bracket) item;
            FragmentManager manager = getSupportFragmentManager();
            BracketDetailFragment bracketDetailFragment = (BracketDetailFragment) manager.findFragmentById(R.id.activity_fragment_bracket_detail);
            bracketDetailFragment.loadBracket(data);
            showMatchFragment();
        }
    }

    MatchListFragment matchFragment;
    public void showMatchFragment(){

        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.activity_bracket_container);

        FragmentManager fm = getSupportFragmentManager();
        matchFragment = (MatchListFragment) fm.findFragmentByTag("MatchListFragment");
        if (matchFragment == null) {
            FragmentTransaction ft = fm.beginTransaction();
            matchFragment = new MatchListFragment();
            ft.add(linearLayout.getId(), matchFragment, "MatchListFragment");
            ft.commit();
        }
    }
}

这是被称为

的片段
public class MatchListFragment extends Fragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //return super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_match_list, container, false);


        LinearLayout.LayoutParams param = (LinearLayout.LayoutParams) view.getLayoutParams();
        param.weight = 1.0f;
        view.setLayoutParams(param);
        return view;
    }
}

看起来重量调整似乎不起作用。这就是atm的样子。

Before click

After click

编辑:XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/activity_bracket_container"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <fragment
        android:id="@+id/activity_fragment_bracket_list"
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:layout_weight="1"
        class="BracketListFragment" />

    <fragment
        android:background="?android:attr/detailsElementBackground"
        android:id="@+id/activity_fragment_bracket_detail"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:layout_width="0dp"
        class="BracketDetailFragment" />


</LinearLayout>

1 个答案:

答案 0 :(得分:0)

以下步骤 步骤1-添加线性布局宽度0dp 步骤2-添加线性布局权重,无论您的设计如何重量= 1 步骤3-添加线性布局根元素权重总和,如weight = 4 现在检查。

我希望这一步能帮到你。