Android以编程方式在父级和布局中添加片段 – 位置

Android以编程方式在父级和布局中添加片段 – 位置,第1张

概述好的,我正在尝试创建一个通用应用,在手机视图中显示3个标签,在平板电脑风景中显示3个标签.因此,我使用线性布局创建了几种不同的 XML布局. 但是,使用片段,我遇到了一个问题 – 我想在该片段中进行选择时删除最左边的列.我相信我可以做到这一点,但事实证明我无法删除XML中声明的片段.所以现在,我想在XML中创建中间和右侧列: <?xml version="1.0" encoding="utf-8" 好的,我正在尝试创建一个通用应用,在手机视图中显示3个标签,在平板电脑风景中显示3个标签.因此,我使用线性布局创建了几种不同的 XML布局.

但是,使用片段,我遇到了一个问题 – 我想在该片段中进行选择时删除最左边的列.我相信我可以做到这一点,但事实证明我无法删除XML中声明的片段.所以现在,我想在XML中创建中间和右侧列:

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tools="http://schemas.androID.com/tools"    androID:ID="@+ID/menu_layout"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:orIEntation="horizontal" >    <fragment        androID:ID="@+ID/classification_fragment"        androID:name="uk.colessoft.androID.hillList.fragments.MenuClassificationFragment"        androID:layout_wIDth="0dp"        androID:layout_height="match_parent"        androID:layout_weight="1"        tools:layout="@layout/List_classifications" >    </fragment>    <fragment        androID:ID="@+ID/map"        androID:layout_wIDth="0dp"        androID:layout_height="match_parent"        androID:layout_weight="1"         /></linearLayout>

然后使用添加第一列

FragmentManager fragmentManager = getSupportFragmentManager();        androID.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();        MenuCountryFragment countryFragment=new MenuCountryFragment();        fragmentTransaction.add(R.ID.menu_layout,countryFragment,"country_menu").commit();

但我有两个问题:

a)上面的代码只将片段放在线性布局的右侧,我无法指定位置.它需要在左边.

b)XML让我可以根据屏幕大小/方向自由定义片段的不同权重.在Fragments中以编程方式设置布局参数似乎是一个倒退的步骤.我通过在xml中放置一个占位符视图来解决这个问题,我从中读取了布局参数并将它们分配给onCreateVIEw,但它是一个黑客.

我知道我可以通过将片段添加为xml中第一个元素的子元素来将片段放在正确的位置但是当我这样做并删除片段时会留下一个空白区域.

解决方法 为要添加的新片段(在XML中)创建一个占位符,如下所示……

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tools="http://schemas.androID.com/tools"    androID:ID="@+ID/menu_layout"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:orIEntation="horizontal" >    <FrameLayout            androID:ID="@+androID:ID/realtabcontent"            androID:layout_wIDth="fill_parent"            androID:layout_height="0dp"            androID:layout_weight="1" />    <fragment        androID:ID="@+ID/classification_fragment"        androID:name="uk.colessoft.androID.hillList.fragments.MenuClassificationFragment"        androID:layout_wIDth="0dp"        androID:layout_height="match_parent"        androID:layout_weight="1"        tools:layout="@layout/List_classifications" >    </fragment>    <fragment        androID:ID="@+ID/map"        androID:layout_wIDth="0dp"        androID:layout_height="match_parent"        androID:layout_weight="1"         /></linearLayout>

然后,不是在运行时添加新片段,而是替换空容器的内容……

FragmentManager fragmentManager = getSupportFragmentManager();androID.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();MenuCountryFragment countryFragment=new MenuCountryFragment();fragmentTransaction.replace(R.ID.realtabcontent,newFrag).commit();fragmentManager.executePendingTransactions();

希望这可以帮助.

总结

以上是内存溢出为你收集整理的Android以编程方式在父级和布局中添加片段 – 位置全部内容,希望文章能够帮你解决Android以编程方式在父级和布局中添加片段 – 位置所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1124838.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存