我想三个字符串格式化成一个机器人按钮。第一个字符串应该是按钮的大约30%,第二个字符串应该是大约50%,其余的是第三个按钮。每个文本应该在一定的长度内被约束。我怎样才能做到这一点?我想多串(不通过XML)编程方式添加到我的Android按钮
通过多种可能的解决方案搜索后,我想出了是要创建3个不同的按钮,并把它们放在一起。这并不理想,但这是一个开始。有没有更好的方法来做到这一点?
这是我到目前为止已经试过:
Java的:
LinearLayout ll = (LinearLayout) findViewById(R.id.linearlayout_buttons);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
Button addButton = new Button(this);
Button numPlayerButton = new Button(this);
Button lobbyButton = new Button(this);
Button locationButton = new Button(this);
ll.addView(addButton, lp);
ll.addView(numPlayerButton, lp);
ll.addView(lobbyButton, lp);
ll.addView(locationButton, lp);
XML:
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/linearlayout_buttonlist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearlayout_buttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- THIS IS WHERE THE BUTTONS GO!!!!!! -->
</LinearLayout>
</LinearLayout>
</ScrollView>
我这段代码的目标是让按键与水平创建内部线性布局,我认为外部线性布局会在下一行(垂直)创建下一个按钮迭代。我有通过循环编程创建的按钮。
欢迎来到Stack Overflow!我可以建议你发布一个最小样本,显示你现在使用的方法,以便其他用户可以看到是否有更好的方法给你? –
我目前的方式只是一个简单的按钮,我还没有执行任何事情,因为我不知道如何处理它。丹尼尔·米的 –
ANS是完美的..如果你不使用片段不是删除仅在第一行的“意见”,指出会为你去工作,与该.. .. –