【发布时间】:2017-07-03 01:05:00
【问题描述】:
我有一个简单的看法:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/contact_selected"
android:layout_marginTop="10dp"
android:layout_marginEnd="5dp"
android:orientation="horizontal"
android:padding="3dp">
<TextView
android:id="@+id/txt_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Billy Bob"
/>
</LinearLayout>
当我将 LinearLayout 标记静态复制到我的主要活动布局中时,边距符合预期。但是,当我动态地将视图添加到主活动布局中时,边距会被忽略。这是我插入视图的方式
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.test, null);
TextView txt_title = (TextView)view.findViewById(R.id.txt_title);
txt_title.setText("Dynamic #1");
llayout.addView(view, llayout.getChildCount()-1);
View view2 = inflater.inflate(R.layout.test, null);
txt_title = (TextView)view2.findViewById(R.id.txt_title);
txt_title.setText("Dynamic #2");
llayout.addView(view2, llayout.getChildCount()-1);
它是这样的:
主布局中的容器是一个LinearLayout,它是一个HorizontalScrollView的子级。任何见解都值得赞赏。
【问题讨论】: