Android的按钮失去其文本对齐 [英] Android Button loses its Text Alignment
问题描述
我有一些按钮和我的布局好手。我玩按钮单击音乐。当按钮然后pssed $ P $按钮上的文字被排列到左。我不知道为什么它的发生。如果我评论点击按钮监听器,然后如果我改变微调的项目,然后还要它得到左对齐。
下面是我的code的一部分。
<?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT> <的LinearLayout
机器人:ID =@ + ID / linearLayout1
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentTop =真正的> <微调
机器人:ID =@ + ID / spnList
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT/>
< / LinearLayout中> < TableLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真
机器人:填充=5DP> <的TableRow
机器人:layout_width =match_parent
机器人:layout_height =match_parent> <按钮
机器人:ID =@ + ID / btnCall
机器人:layout_weight =1
机器人:文字=C/> <按钮
机器人:ID =@ + ID / btnCut
机器人:layout_weight =1
机器人:文字=X/>
< /&的TableRow GT; <的TableRow
机器人:layout_width =match_parent
机器人:layout_height =match_parent> <按钮
机器人:ID =@ + ID / BTN1
机器人:layout_weight =1
机器人:文字=1/> <按钮
机器人:ID =@ + ID / BTN2
机器人:layout_weight =1
机器人:文字=2/> <按钮
机器人:ID =@ + ID / btn3
机器人:layout_weight =1
机器人:文字=3/>
< /&的TableRow GT;
< / TableLayout>< / RelativeLayout的>
下面是活动code:
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main1);
BTN1 =(按钮)findViewById(R.id.btn1);
BTN2 =(按钮)findViewById(R.id.btn2);
btn3 =(按钮)findViewById(R.id.btn3);
spnList =(微调)findViewById(R.id.spnList);
清单<串GT;名单=新的ArrayList<串GT;();
list.add(第一);
list.add(二);ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(
BabyMobile1.this,android.R.layout.simple_spinner_item,清单);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);spnList.setAdapter(适配器);
spnList.setOnItemSelectedListener(新OnItemSelectedListener(){ 公共无效onItemSelected(适配器视图<>为arg0,ARG1查看,
INT ARG2,长ARG3){
// TODO自动生成方法存根
activeItem =(字符串)arg0.getItemAtPosition(ARG2);
}公共无效onNothingSelected(适配器视图<>为arg0){
// TODO自动生成方法存根 }
});
您没有定义的宽度和高度上的按钮属性。试着用添加以下属性。
的android:layout_width =0dp
机器人:layout_height =WRAP_CONTENT
例如:
<按钮
机器人:ID =@ + ID / btnCall
机器人:layout_weight =1
机器人:文字=C
机器人:layout_width =0dp
机器人:layout_height =WRAP_CONTENT/>
编辑:回答上面给出的解决方案,以保持按键中心horizantal但他们的文本的文本上移时,我测试。我搜索这一阵,发现在堆栈类似的问题(还没有回答)。我想有一个小的错误使用微调
S(或对话框)在 RelativeLayout的
时。我想,下面来改变你的布局(更改根的LinearLayout
)和它的工作如预期。如果你有机会跟我来代替你的布局(图)您的问题将得到solveld。但我不知道为什么使用 RelativeLayout的
导致这样的问题。
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直> <的LinearLayout
机器人:ID =@ + ID / linearLayout1
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT> <微调
机器人:ID =@ + ID / spnList
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT/>
< / LinearLayout中> <查看
机器人:layout_width =match_parent
机器人:layout_height =0dp
机器人:layout_weight =1/> < TableLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT> <的TableRow
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT> <按钮
机器人:ID =@ + ID / btnCall
机器人:layout_width =0dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:文字=C/> <按钮
机器人:ID =@ + ID / btnCut
机器人:layout_width =0dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:文字=X/>
< /&的TableRow GT; <的TableRow
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT> <按钮
机器人:ID =@ + ID / BTN1
机器人:layout_width =0dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:文字=1/> <按钮
机器人:ID =@ + ID / BTN2
机器人:layout_width =0dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:文字=2/> <按钮
机器人:ID =@ + ID / btn3
机器人:layout_width =0dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:文字=3/>
< /&的TableRow GT;
< / TableLayout>< / LinearLayout中>
I have some buttons and a Spinner in my Layout. I Play Music on button click. When Button is pressed then button text gets aligned to left. I don't know why its happening. If I comment button click listener and then if I change items in Spinner , then also it gets Left Aligned.
Here is the part of my code.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<Spinner
android:id="@+id/spnList"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:padding="5dp" >
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/btnCall"
android:layout_weight="1"
android:text="C" />
<Button
android:id="@+id/btnCut"
android:layout_weight="1"
android:text="X" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/btn1"
android:layout_weight="1"
android:text="1" />
<Button
android:id="@+id/btn2"
android:layout_weight="1"
android:text="2" />
<Button
android:id="@+id/btn3"
android:layout_weight="1"
android:text="3" />
</TableRow>
</TableLayout>
</RelativeLayout>
Here is Activity Code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
spnList = (Spinner) findViewById(R.id.spnList);
List<String> list = new ArrayList<String>();
list.add("First");
list.add("Second");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
BabyMobile1.this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnList.setAdapter(adapter);
spnList.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
activeItem = (String) arg0.getItemAtPosition(arg2);
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
You are not defining width and height attributes on your buttons. Try with adding below attributes.
android:layout_width="0dp"
android:layout_height="wrap_content"
Example:
<Button
android:id="@+id/btnCall"
android:layout_weight="1"
android:text="C"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
Edit: Answer above gives a solution to keep texts of buttons center horizantal but their texts are shifted up when I tested. I searched this a while and found similar problem (not answered yet) at stack. I guess there is a small bug when using Spinner
s(or dialogs) inside a RelativeLayout
. I tried to change your layout below(changed root to LinearLayout
) and it worked as expected. If you have a chance to replace your layout with mine(below) your problem will be solveld. But I have no idea why using RelativeLayout
causes a problem like this.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Spinner
android:id="@+id/spnList"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btnCall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="C" />
<Button
android:id="@+id/btnCut"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="X" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<Button
android:id="@+id/btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<Button
android:id="@+id/btn3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
</TableRow>
</TableLayout>
</LinearLayout>
这篇关于Android的按钮失去其文本对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!