Android LinearLayout / TableLayout 与自定义 ListAdapter。制作图书预览列表项目
我已经使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
稍微笨拙的解决方法:
如果您只查看简单的字体更改(粗体/斜体/下划线),您可以在同一文本视图中使用 HTML 格式(例如< b/>等)。您可以通过使用
author.setCompoundDrawablesWithIntrinsicBounds(BookCoverDrawable, null, null, null); 设置图像来进一步优化布局。
Slightly clumsy work around:
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);