我的问题可能是,"如何使用两个单行非包装TextView创建单行水平LinearLayout,其中左TextView始终占据可用屏幕宽度的1/3,右TextView始终占据2/3可用的屏幕宽度,当文本标签太长而无法显示时,文本标签将被截断?" (如果这个问题足够清楚且读者 - 那就是你 - 已经考虑过他们愿意分享的解决方案,那么进一步阅读问题和问题描述可能是不必要的.)
填充了四个这样的LinearLayouts的ListView看起来像这样:
medium length text ---- short text
short text ---- text that is too loooooooooooooo...
loooooooooooooo... ---- short text
loooooooooooooo... ---- text that is too loooooooooooooo...Run Code Online (Sandbox Code Playgroud)在这个模型中,在第一行中,左右文本标签足够短以完全显示,在第二行中,左标签足够短以完全显示,而右标签的文本太长并且因此被截断,并且右侧标签的文本的开头与第一行的右侧文本垂直对齐,在视觉上创建一个列,就好像它在TableLayout中一样,在第三行中,左侧标签的文本也是如此long被截断,以便与第二行和第一行的左侧文本垂直对齐,右侧标签的文本开始与第二行和第一行的右侧文本垂直对齐,第四行中的文本与左侧标签太长,因此与第三行的左侧文本类似地显示,并且右侧标签的文本太长,因此与第二行的右侧文本类似地显示.
从stackoverflow和其他地方的各个帖子,我收集到,虽然使用带有ArrayAdapter的TableLayout有点可能,但有一些缺点,它可能不是正确的方法.
我当然尝试过许多布局参数组合,但目前还没有任何一个接近目标.我试图找出一个适用于许多不同屏幕尺寸的解决方案,因此指定特定像素宽度可能不会很好.
也许以下简单的代码示例(带有屏幕截图)是解决方案的一个很好的起点,只需要一些小的修改.
(stackoverflow代码格式化程序对此代码感到不满.所以,请原谅任何时髦的格式.)
public class TableLayoutLikeListView extends ListActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
List<Datum> data = new ArrayList<Datum>();
data.add(new Datum("short", "short"));
data.add(new Datum("short", "loooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnnng"));
data.add(new Datum("loooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnnng", "short"));
data.add(new Datum("loooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnnng",
"loooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnnnnnnnnnnnnng"));
setListAdapter(new MyAdapter(this, R.layout.row, data)); …Run Code Online (Sandbox Code Playgroud) 我在xml中有一个包含一个TableLayout的ScrollView.我的问题是,每次点击它时是否可以有一个可聚焦的行.这是我的xml代码:
<ScrollView
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="@+id/table2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableRow>
<RelativeLayout
android:id="@+id/rowlayout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@drawable/rowbackground2" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_code_contact"
android:padding="7dip"
android:layout_alignParentLeft="true" />
<TextView
android:id="@+id/contacts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Contacts"
android:textColor="#000"
android:textSize="18dip"
android:layout_toRightOf="@id/icon"
android:paddingTop="10dip" />
</RelativeLayout>
</TableRow>
<TableRow>
<Button
android:id="@+id/contacts_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/contact_button" />
</TableRow>
Run Code Online (Sandbox Code Playgroud)
我已经尝试了"focusable ="true""和"focusableInTouchMode ="true""但没有发生任何事情.
提前致谢