Android LinearLayout / TableLayout 与自定义 ListAdapter。制作图书预览列表项目

发布于 2024-09-15 07:17:23 字数 1084 浏览 1 评论 0原文

我已经使用RelativeLayout 来自定义ListAdapter 的实现,但我不确定是否可以继续使用它或者是否需要使用TableLayout。

在第一个示例中,我通过使用一个图像视图和一个包含书名和作者姓名的文本视图来按我想要的方式放置文本。但是,我想以不同的方式设置作者文本的样式,因此我认为我需要两个文本视图。

在第二个示例中,我添加了另一个文本视图,但它浮动到右侧。第二个例子是Clipboard02.png。

如何使第二个文本视图位于第一个文本下方(例如1)。我是用代码而不是使用 XML 布局来完成此操作的。

http://carriehall.co.uk/Clipboard01.png

LinearLayout.LayoutParams skyParams = new LinearLayout.LayoutParams( 70,
    LayoutParams.WRAP_CONTENT );

ImageView skyControl = new ImageView( context );
skyControl.setImageResource( R.drawable.the_eyre_affair );
addView( skyControl, skyParams );

LinearLayout.LayoutParams bookParams = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT,
    LayoutParams.WRAP_CONTENT);
bookParams.setMargins( 5, 10, 5, 10 );

TextView bookControl = new TextView( context );
bookControl.setTextAppearance( context, R.style.SpecialText );
bookControl.setText( book.getTitle( ) + "\n\n" + book.getAuthor( ));

addView( bookControl, bookParams );

I have used RelativeLayout for a custom implementation of a ListAdapter and I am not sure if I can continue using this or if I need to use TableLayout.

In the first example I have the text positioned as I would like by using one image view and one text view containing the book name and the author name. However, I want to style the author text differently so I think I will need two text views.

In the second example I have added another text view but it floats to the right. Second eg is Clipboard02.png.

How can I make the second text view go under the first text (as in eg 1). I have done this in code rather than using XML layout.

http://carriehall.co.uk/Clipboard01.png

LinearLayout.LayoutParams skyParams = new LinearLayout.LayoutParams( 70,
    LayoutParams.WRAP_CONTENT );

ImageView skyControl = new ImageView( context );
skyControl.setImageResource( R.drawable.the_eyre_affair );
addView( skyControl, skyParams );

LinearLayout.LayoutParams bookParams = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT,
    LayoutParams.WRAP_CONTENT);
bookParams.setMargins( 5, 10, 5, 10 );

TextView bookControl = new TextView( context );
bookControl.setTextAppearance( context, R.style.SpecialText );
bookControl.setText( book.getTitle( ) + "\n\n" + book.getAuthor( ));

addView( bookControl, bookParams );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野心澎湃 2024-09-22 07:17:24

稍微笨拙的解决方法:

LinearLayout ll = new LinearLayout(getApplicationContext());
            ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            ll.setOrientation(LinearLayout.VERTICAL);

            TextView author = new TextView(getApplicationContext());
            TextView book = new TextView(getApplicationContext());

            LayoutParams params =  new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            ll.addView(author, params);
            ll.addView(book, params);

如果您只查看简单的字体更改(粗体/斜体/下划线),您可以在同一文本视图中使用 HTML 格式(例如< b/>等)。您可以通过使用 author.setCompoundDrawablesWithIntrinsicBounds(BookCoverDrawable, null, null, null); 设置图像来进一步优化布局。

Slightly clumsy work around:

LinearLayout ll = new LinearLayout(getApplicationContext());
            ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            ll.setOrientation(LinearLayout.VERTICAL);

            TextView author = new TextView(getApplicationContext());
            TextView book = new TextView(getApplicationContext());

            LayoutParams params =  new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            ll.addView(author, params);
            ll.addView(book, params);

If you are only looking at simple font change (Bold/Italics/Underline) you can use HTML formatting (eg.< b/> etc) in the same text view. You can further optimize the layout by setting the image using author.setCompoundDrawablesWithIntrinsicBounds(BookCoverDrawable, null, null, null);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文