如何通过代码添加分隔符视图

时间:2017-02-16 20:47:49

标签: android

Container是LinearLayout,

<LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

在代码中,我尝试将视图添加为:

final LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

// add alpha view to container view
View alphaView = inflater.inflate(R.layout.item_info_alpha, null, false);
container.addView(alphaView);

// add divider view to container view
container.addView(inflater.inflate(R.layout.item_divider, null, false));

// add beta view to container view
View betaView = inflater.inflate(R.layout.item_info_beta, null, false);
container.addView(betaView);

// add divider view to container view
container.addView(inflater.inflate(R.layout.item_divider, null, false));

item_divider.xml

<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="8dp"
    android:background="#F2F2F4" />

通过代码添加时,分隔符不可见。

如何在将视图添加到线性布局时使此分隔符可见?

为什么当我们通过xml添加查看时,它没有在LinearLayout中累加?

2 个答案:

答案 0 :(得分:1)

你可以这样做:

container.addView(inflater.inflate(R.layout.item_divider, null));

或尝试此代码:

        View view = new View(YOUR_CONTEXT); //getContext() for example
        int hight = 8;
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, hight);
        view.setLayoutParams(params);
        view.setBackgroundColor(Color.parseColor("#F2F2F4"));
        container.addView(view);

答案 1 :(得分:0)

<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> <nav class="aptest-sidenav aptest-collapse aptest-white aptest-animate-left" style="z-index:3;width:300px;" id="mySidenav">This is my side nav <br /> <div class="aptest-container aptest-row"> <div class="aptest-col s4"> <img src="/aptestimages/avatar2.png" class="aptest-circle aptest-margin-right" style="width:46px" /> </div> <div class="aptest-col s8"> <span>Welcome, <strong><asp:Label runat="server" ID="Name_txt" Text="" /></strong></span><br /> </nav> <div class="aptest-overlay aptest-hide-large aptest-animate-opacity" onclick="aptest_close()" style="cursor:pointer" title="close side menu" id="myOverlay"></div> <!-- !PAGE CONTENT! --> <div class="aptest-main" style="margin-left:300px;margin-top:43px;"> </div> <input type="button" id="aptest_open" value="aptest_open" /> <script type="text/javascript"> $(document).ready(function () { var mySidenav = $("#mySidenav"); var overlayBg = $("#myOverlay"); $("#aptest_open").click(function () { if (mySidenav.css('display') === 'block') { mySidenav.css('display', 'none'); } else { alert("2"); mySidenav.css('display', 'block'); } }); function aptest_close() { alert(); mySidenav.style.display = "none"; overlayBg.style.display = "none"; } }); </script>中的false替换为inflater.inflate(..),不要在true上明确致电addView(..)

LinearLayout的目的是不必手动设置LayoutInflater

如果您使用LayoutParamsView进行充气,则会使用attachToRoot=false根来创建新的LayoutParams,然后将其丢弃。

如果View,则会保留attachToRoot=true,并且新的LayoutParams会附加到rootView,例如View