为什么动态创建布局与在xml中创建布局不同?

时间:2016-10-13 19:05:48

标签: java android xml android-layout orientation

这是我为布局创建的xml文件,它显示为我想要的内容。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="120dp"
android:background="@android:drawable/dialog_holo_light_frame">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <ImageView
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:id="@+id/imageSectionCustomRowL"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:paddingLeft="10dp"
        android:id="@+id/titleCustomRowL"
        android:paddingRight="10dp"
        android:textSize="30sp"
        android:textColor="#000000"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="#959595"
            android:paddingBottom="10dp"
            android:paddingTop="5dp"
            android:id="@+id/subTitleCustomRowL"
            android:paddingLeft="20dp"
            android:paddingRight="10dp"/>
    </LinearLayout>
</LinearLayout>
</LinearLayout>

这或多或少是相同的,但通过代码动态创建

LinearLayout parentLL = (LinearLayout)findViewById(R.id.newsMainPopulationLL);
    LinearLayout parentRowLL = new LinearLayout(news.this);
    parentRowLL.setOrientation(LinearLayout.VERTICAL);
    Drawable bg = news.this.getResources().getDrawable(android.R.drawable.dialog_holo_light_frame);
    //Drawable bbg = getDrawable(android.R.drawable.dialog_holo_light_frame);
    parentRowLL.setBackgroundResource(android.R.drawable.dialog_holo_light_frame);
    parentRowLL.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertDpToPx(120)));
    LinearLayout parentRowChildHLL = new LinearLayout(news.this);
    parentRowChildHLL.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    parentRowChildHLL.setOrientation(LinearLayout.HORIZONTAL);
    LinearLayout containerForPic = new LinearLayout(news.this);
    containerForPic.setLayoutParams(new LinearLayout.LayoutParams(convertDpToPx(75), convertDpToPx(75)));
    containerForPic.setOrientation(LinearLayout.VERTICAL);
    ImageView picInList = new ImageView(news.this);
    picInList.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    LinearLayout contentVerticalLL = new LinearLayout(news.this);
    contentVerticalLL.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    contentVerticalLL.setOrientation(LinearLayout.VERTICAL);
    TextView headingTV = new TextView(news.this);
    headingTV.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    headingTV.setPadding(convertDpToPx(10), convertDpToPx(10), convertDpToPx(10), 0);
    headingTV.setTextColor(Color.parseColor("#000000"));
    headingTV.setTextSize(30);
    TextView contentTV = new TextView(news.this);
    contentTV.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    contentTV.setPadding(convertDpToPx(10), convertDpToPx(5), convertDpToPx(10), convertDpToPx(10));
    contentTV.setTextColor(Color.parseColor("#959595"));
    contentVerticalLL.addView(headingTV);
    contentVerticalLL.addView(contentTV);
    parentRowChildHLL.addView(contentVerticalLL);
    containerForPic.addView(picInList);
    parentRowChildHLL.addView(containerForPic);
    parentRowLL.addView(parentRowChildHLL);
    parentLL.addView(parentRowLL);

这里convertDptoPx是一个正常工作的int()。但是由于某种原因,线性布局parentRowChildLL即使因某种原因设置为LinearLayout.HORIZONTAL因为某种原因而垂直显示(根据我使用开发人员选项设置看到的布局边界)

有什么基本的东西我错过了吗?当我看到边界时,我有3行。而且我也尝试在布局中包含我的图像视图,否则它甚至都不可见。

谢谢。

0 个答案:

没有答案