【问题标题】:Android layouts - programmatically setting value for a custom layout componentsAndroid 布局 - 以编程方式为自定义布局组件设置值
【发布时间】:2013-07-17 11:57:51
【问题描述】:

我定义了一个简单的自定义布局,其中包括一个文本视图和一个图像视图。在我的主布局中,我想多次使用这个布局,并且我想在我的代码中为这些文本和图像视图添加价值。 (现在手动,但后来从数据库中获取数据)

如何在我的代码中访问这些组件?

这是我的主要布局 xml 文件

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/comment_content"
    android:orientation="horizontal" >

    <!-- these four components have the same structure but different styles and behaviors. -->
    <include
        android:id="@+id/like"
        style="@style/LikeStyle"
        android:layout_width="0dip"
        android:layout_weight="1"
        layout="@layout/comment_actions" />

    <include
        android:id="@+id/dislike"
        style="@style/DislikeStyle"
        android:layout_width="0dip"
        android:layout_weight="1"
        layout="@layout/comment_actions" />

    <include
        android:id="@+id/reply"
        style="@style/ReplyStyle"
        android:layout_width="0dip"
        android:layout_weight="1"
        layout="@layout/comment_actions" />

    <include
        android:id="@+id/share"
        style="@style/ShareStyle"
        android:layout_width="0dip"
        android:layout_weight="1"
        layout="@layout/comment_actions" />
</LinearLayout>

以及适配器中我要设置数据的部分:

public View getView(int position, View view, ViewGroup parent) {
    ViewHolder holder = null;
    if (view == null) {
        LayoutInflater infalInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = infalInflater.inflate(R.layout.comment_item, null);
        holder = new ViewHolder();
        holder.userName = (TextView) view.findViewById(R.id.user_name);
        holder.userImage = (ImageView) view.findViewById(R.id.user_image);
        holder.commentContent = (TextView) view.findViewById(R.id.comment_content);
        holder.commentTitle = (TextView) view.findViewById(R.id.comment_title);

        // these tree below layouts are custom. now when running my app i recieve a class cast exception
        // because r.id.like and other twos are relative layout.
        holder.likes = (TextView) view.findViewById(R.id.like);
        holder.dislikes = (TextView) view.findViewById(R.id.dislike);
        holder.replys = (TextView) view.findViewById(R.id.reply);
        view.setTag(holder);
    }
    else {
        holder = (ViewHolder) view.getTag();
    }
    holder.userName.setText(((Comment) listData.get(position)).getUsername());
    holder.commentContent.setText(((Comment)listData.get(position)).getContent());
    holder.commentTitle.setText(((Comment)listData.get(position)).getTitle());

    // the part where I want to set data for these custom layouts.
    holder.likes.setText(((Comment) listData.get(position)).getLikes());
    holder.dislikes.setText(((Comment) listData.get(position)).getDislikes());
    holder.replys.setText(((Comment) listData.get(position)).getReplys());
    return view;
}

【问题讨论】:

  • 更好的方法是编写一个自定义 View 类,具有自己的布局和属性。
  • 谢谢。我会试试的。

标签: android xml layout customization


【解决方案1】:

您必须将其转换为 RelativeLayout。转到comment_actions 布局并检查TextView 是否设置了ID。如果没有,请这样做。

然后将您的喜欢、回复和内容投到RelativeLayout 并从那里找到TextView

RelativeLayout rlLikes = (RelativeLayout) view.findViewById(R.id.like);
holder.likes = (TextView) rlLikes.findViewById(R.id.idOfTextViewInsideComment_actions);

【讨论】:

  • 是的。 TextView 和 ImageView 都有 ID。非常感谢您帮助我。
  • 那行得通,现在我已经正确设置了值。但是当我运行代码时,我看到一个数字(喜欢数字)而不是 4 个数字和 4 个 4 图像(似乎它们在彼此之上)你知道那是什么吗?
  • 不,不是。我想这与comment_actions 内部的布局有关。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-31
  • 2011-11-20
  • 2015-12-26
相关资源
最近更新 更多