【问题标题】:Setting layout parameters for an element inside a Gallery为图库中的元素设置布局参数
【发布时间】:2011-08-17 00:41:22
【问题描述】:

我正在使用Gallery 来显示事件的水平时间线。一些事件得到Gravity.TOP 和一些Gravity.BOTTOM 将它们对齐在显示年份的漂亮线条之上或之下。到目前为止,一切顺利。

我想改变顶部元素的左边距属性,所以没有巨大的间隙并且元素看起来交错。例如:为每个在顶部对齐的元素设置负左边距。

Gallery 的每个元素都包含在一个 LinearLayout 上,可以设置一个 MarginLayoutParams 实例以编程方式更改边距。但是,在适配器内部使用MarginLayoutParams 时,我得到了ClassCastException,因为库代码这样做:

    // Respect layout params that are already in the view. Otherwise
    // make some up...
    Gallery.LayoutParams lp = (Gallery.LayoutParams) child.getLayoutParams();

关于如何克服这个问题的任何想法或提示?

【问题讨论】:

  • “我在适配器内部使用 MarginLayoutParams 时遇到 ClassCastException” - 你能告诉我们你的 MarginLayoutParams 是如何与你的适配器交互的吗?

标签: android android-layout android-gallery


【解决方案1】:

图库的每个元素都包含一个 LinearLayout

只需使用另一个LinearLayout 将其包裹起来,并在LinerLayout.LayoutParams 中为内部LinearLayout 设置边距。我已经检查过了,它似乎做你想要的。

因此,您为图库项目充气的布局应该如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layOuter"
    android:layout_width="wrap_content" android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/layInner"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView android:id="@+id/imageView1" android:src="@drawable/icon"
            android:layout_height="wrap_content" android:layout_width="wrap_content"
            android:scaleType="fitXY" />

        <TextView android:text="TextView" android:id="@+id/textView"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:visibility="visible" />
    </LinearLayout>
</LinearLayout>

然后您可以在适配器getView 方法中访问内部LinearLayout,并根据您的条件设置margin(示例代码没有convertView重用优化):

public View getView(int position, View convertView, ViewGroup parent) {
  Context context = getContext();
  final float density = context.getResources().getDisplayMetrics().density;

  LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  View layOuter = inflater.inflate(R.layout.row_layout, null);
  View layInner = layOuter.findViewById(R.id.layInner);
  if (...) {  // your condition
    LinearLayout.LayoutParams innerLP = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    innerLP.leftMargin = (int) (50 * density);
    layInner.setLayoutParams(innerLP);
  }
  return layOuter;
}

请注意,内部布局必须使用LinearLayout.LayoutParams(它扩展MarginLayoutParams),否则将不起作用。

【讨论】:

  • 我需要的是两个选项的混合。我必须在 XML 文件中引用 LinearLayout 并以编程方式设置边距,因为不同的元素会有不同的边距。那是你得到异常的时候。
  • 请查看更新后的答案。作为一般规则:您可以为布局内支持边距的元素设置边距(例如:RelativeLayout、LinearLayout、FrameLayout)。因此,在您的情况下,您可以为放置在 LinearLayout 中的元素设置边距(但不能为 LinearLayout 本身,当它的父级是不支持为其子级设置边距的 Gallery 时 - 这就是我们应该添加内部 LinearLayout 的原因)。如果您需要其他帮助,请发布您用于图库行的布局和getView 代码。
  • 添加一层子元素有效。您似乎无法更改 Gallery 的直接子级的布局参数。谢谢!
  • 我无法在 4 小时内奖励赏金(我猜是因为它不到 24 小时),但我会在启用后立即奖励给您。 :)
  • 很高兴听到它对您有所帮助。关于赏金的 NP - 我可以等 4 个小时。 ;)
【解决方案2】:

Gallery.LayoutParams 是一个与 android.view.ViewGroup.LayoutParams 完全不同的类。

您的子视图(由适配器构建)是一个返回 android.view.ViewGroup.LayoutParams 的 LinearLayout,而 Gallery 返回一个 Gallery.LayoutParams。

尝试使用 android.view.ViewGroup.LayoutParams 而不是 Gallery.LayoutParams。如果您必须同时使用两者,则在适用的情况下手动将属性从一个设置为另一个(尽管我真的想不出您需要同时使用两者的原因)。

【讨论】:

  • 我没有使用 Gallery.LayoutParams,这就是整个问题。我的 LinearLayout 会自动包装在 Gallery.LayoutParams 中,因为它是 Gallery 小部件中的一个元素。这正是问题所在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多