1

レイアウトにいくつかのボタンとスピナーがあります。ボタンクリックで音楽を再生します。Button が押されると、ボタンのテキストが左揃えになります。なぜそれが起こっているのかわかりません。コメント ボタン クリック リスナーの場合、 Spinner の項目を変更すると、左揃えになります。

これが私のコードの一部です。

<?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>

アクティビティ コードは次のとおりです。

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

    }
});
4

3 に答える 3

1

ボタンの幅と高さの属性を定義していません。以下の属性を追加してみてください。

android:layout_width="0dp"
android:layout_height="wrap_content"

例:

<Button
    android:id="@+id/btnCall"
    android:layout_weight="1"
    android:text="C" 
    android:layout_width="0dp"
    android:layout_height="wrap_content"/>

編集:上記の回答は、ボタンのテキストを水平に保つための解決策を提供しますが、テストするとテキストが上に移動します。これをしばらく検索したところ、スタックで同様の問題(まだ回答されていません)が見つかりました。Spinner内で s(またはダイアログ)を使用する場合、小さなバグがあると思いますRelativeLayout。以下のレイアウトを変更しようとしました(ルートをに変更しましたLinearLayout)が、期待どおりに機能しました。あなたのレイアウトを私のもの(以下)に置き換える機会があれば、あなたの問題は解決されます. RelativeLayoutしかし、なぜ使用するとこのような問題が発生するのかわかりません。

<?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>
于 2013-11-04T10:26:16.743 に答える
0

ボタンとスピナーの配置を確認します。スピナーでカスタム アイテムを使用している場合は、カスタム アイテムの配置も確認する必要があります。

目的をよりよく理解するために、何を書いているのか、どこで問題が発生しているのかをコードで教えてください。

于 2013-11-04T09:07:11.133 に答える