【问题标题】:Inflating RelativeLayout View into horizontal scroll view deletes layout settings将 RelativeLayout 视图膨胀为水平滚动视图会删除布局设置
【发布时间】:2015-03-15 10:51:53
【问题描述】:

抱歉,这个问题太长了..
我将元素的相对布局动态添加到 Horizo​​ntalscrollview 的 Linearlayout 中。
当我看到布局时,预览看起来很棒,但是当我以编程方式添加它时,它会破坏视图.. 文本移动到布局上的另一个位置,并且填充被忽略..

这是相对布局元素 deal_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="310dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="15dp"
    android:layout_marginRight="15dp"
    android:id="@+id/deal_layout"
    >
    <ImageView
        android:layout_width="310dp"
        android:layout_height="130dp"
        android:src="@drawable/deal_square"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:contentDescription="deals background" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:paddingTop="25dp"
        android:id="@+id/from_to_deals_txt"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="from "
            android:textAppearance="@style/from_to_deals"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Source"
            android:textAppearance="@style/source_deals"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" to "
            android:textAppearance="@style/from_to_deals"
            />
    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="destination"
        android:textAppearance="@style/dst_deals"
        android:layout_below="@id/from_to_deals_txt"
        android:layout_alignRight="@id/from_to_deals_txt"
        android:paddingTop="10dp"
        android:id="@+id/dst_deals_txt"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1099 X"
        android:textAppearance="@style/price_deals"
        android:layout_below="@id/dst_deals_txt"
        android:layout_alignRight="@id/from_to_deals_txt"
        android:layout_alignEnd="@id/from_to_deals_txt"
        android:paddingTop="10dp"
        android:id="@+id/price_deal_txt"
        />

    <ImageView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:src="@drawable/ic_launcher"
        android:background="#000000"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        />
</RelativeLayout>

这是水平滚动视图:

<?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="fill_parent"
    android:background="@drawable/bg"
    android:padding="10dp">
    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/dealsView"
            android:paddingTop="10dp"
            >

        </LinearLayout>
    </HorizontalScrollView>
</LinearLayout>

我正在使用这个 java 函数添​​加 Relativelayout 元素:

LinearLayout dealsView;
dealsView = (LinearLayout)findViewById(R.id.dealsView);
for(int i=0;i<5;i++)
{
    LayoutInflater mInflater ;
    mInflater = LayoutInflater.from(this);
    View cur_deal = mInflater.inflate(R.layout.deal_layout, null);
    dealsView.addView(cur_deal);
}

在预览中看起来像这样:

在应用程序上看起来像这样:

有人知道我做错了什么吗?

当我将带有复制粘贴的元素添加到线性布局时,它看起来很棒,并且边距工作正常,只是从代码中它不起作用..

谢谢!

【问题讨论】:

  • 而不是传递 R.layout.your_layout, null 给充气机传递 R.layout.your_layout, dealView, false
  • 有效,你能不能把它写成答案,我会标记它,你能解释一下方法之间的区别吗?谢谢。

标签: android android-layout layout android-relativelayout horizontalscrollview


【解决方案1】:

不要将 null 传递给 inflater,而是传递 Views 预期的父级 (dealsView),以便可以使用来自根的 LayoutParams 进行膨胀。

View cur_deal = mInflater.inflate(R.layout.deal_layout, dealsView, false);

我相信这个问题是由您所有使用 width="wrap_content" 的视图引起的,但如果 inflater 不知道根,它就无法正确地放大该值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 2019-04-05
    • 1970-01-01
    相关资源
    最近更新 更多