繁体   English   中英

使用按钮更改片段的大小

[英]Change the size of a fragment with a button

当我单击mapview片段内的按钮时,我正在尝试更改“ MapView”和“ InfoContent”的权重。 mapView Fragment在PlaceholderFragment2 Java类中定义。 第一次单击应将权重更改为新值,而第二次单击应将原值恢复。

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" 
android:orientation="vertical"
>

<FrameLayout
    android:id="@+id/TitleBar"
    android:layout_width="fill_parent"
    android:layout_height="50dp" 
    >
</FrameLayout>

<FrameLayout
    android:id="@+id/MapView"
    android:layout_width="fill_parent"
    android:layout_height="0dp" 
    android:layout_weight="1">
</FrameLayout>

<FrameLayout
    android:id="@+id/InfoTitleBar"
    android:layout_width="fill_parent"
    android:layout_height="170dp" 
    >
</FrameLayout>

<FrameLayout
    android:id="@+id/InfoContent"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="0" 
    >
</FrameLayout></LinearLayout>

MainActivity.Java

public class MainActivity extends Activity {

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

    PlaceholderFragment frg=new PlaceholderFragment();
    PlaceholderFragment2 frg2=new PlaceholderFragment2();
    PlaceholderFragment3 frg3=new PlaceholderFragment3();
    PlaceholderFragment4 frg4=new PlaceholderFragment4();

    FragmentManager manager=getFragmentManager();
    FragmentTransaction transaction=manager.beginTransaction();

    transaction.add(R.id.TitleBar, frg, "Frag_Top_tag");
    transaction.add(R.id.MapView, frg2, "Frag_Middle_tag");
    transaction.add(R.id.InfoTitleBar, frg3, "Frag_Bottom_tag");
    transaction.add(R.id.InfoContent, frg4, "Frag_Content_tag");

    transaction.commit();
}

还有PlaceHolderFragment2.java

public static class PlaceholderFragment2 extends Fragment {

    public PlaceholderFragment2() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.mapview, container,
                false);

        final Button button = 
                 (Button) rootView.findViewById(R.id.zoomMap);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    buttonClicked(v);
                }
            });

        return rootView;
    }

我解决了这个问题,将片段放置在线性布局中 ,然后在创建对象的主要活动中,当我单击更改线性布局的权重大小时,cos既不需要也不需要片段事务

主要活动

private Button button;
private LinearLayout layoutListView;
private LinearLayout.LayoutParams lpListView;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_container);

    button = (Button) findViewById(R.id.button);

    layoutListView = (LinearLayout) findViewById(R.id.listTreeView);

    lpListView = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT);

    lpListView.weight = 20;



    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            if (lpListView.weight == 0) {



                lpListView.weight = 20;




            } else {

                lpListView.weight = 0;


            }

            layoutListView.setLayoutParams(lpListView);




        }
    });



}

}

     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="100"
        android:orientation="horizontal"
        android:weightSum="100" >    

        <LinearLayout
            android:id="@+id/listTreeView"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="20"
            android:orientation="horizontal" >

            <fragment
                android:id="@+id/fraglistView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                class="com.example.ListTreeView" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/fragbutton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="80"
            android:background="#F000" >

           <fragment
                android:id="@+id/fraglistRight"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                class="com.example.ListViewRight" />
        </LinearLayout>
</LinearLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM