2

Androidアプリの開発を始めたばかりです。レイアウトの質問があります。アプリのメイン画面を作成したい。これは 7 つのオプションを持つメニューで、各オプションは左側にアイコン、短いテキスト、左側にチェック (オン/オフ コンポーネント) があります。

リストビュー要素に書きました。このレイアウトで単純なアダプターを作成しました:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

       <ImageView android:id="@+id/icon"
            android:layout_width="?android:attr/listPreferredItemHeight"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:src="@drawable/ic_action_io"/>

       <TextView android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/icon"
            android:layout_alignParentTop="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:singleLine="true" />



</RelativeLayout>

メニューには常に 7 つのオプションがあります。リストビューが画面の高さいっぱいになるようにしたいと思います。同じ高さの各要素。リストビューでそれは可能ですか?それとも、リスト ビューからメニューを作成したほうがよいでしょうか?

線形レイアウトと重みプロパティについて読んでいます。助けてください。これは私の最初のレイアウトです。使用すべきレイアウトについてアドバイスをいただければ幸いです。

どうもありがとう、よろしく!

PD: 私の英語でごめんなさい。

4

3 に答える 3

1

すべてのアイテムを(スクロールせずに)表示したい場合は、ListView. 代わりにa を使用し、LinearLayout各メニュー項目の layout_weight を 1 に設定します。

于 2013-04-30T16:05:46.093 に答える
1

Linearlayout を使用し、表示するすべてのアイテムを内部に配置します...

このようなもの:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<RelativeLayout android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >

   <ImageView android:id="@+id/icon"
        android:layout_width="?android:attr/listPreferredItemHeight"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:src="@drawable/ic_launcher"/>

   <TextView android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/icon"
        android:layout_alignParentTop="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Texto"
        android:singleLine="true" />

</RelativeLayout>

<RelativeLayout android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >

   <ImageView android:id="@+id/icon"
        android:layout_width="?android:attr/listPreferredItemHeight"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:src="@drawable/ic_launcher"/>

   <TextView android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/icon"
        android:layout_alignParentTop="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Texto"
        android:singleLine="true" />

</RelativeLayout>

    <!-- Repeat the item five times more -->

</LinearLayout>
于 2013-04-30T16:13:41.160 に答える
1

他の人が言ったように、LinearLayout を使用することをお勧めします。そして、あなたが言ったように、 weight 属性も使用できます。

均等に重み付けされた子供

各子が画面上で同じ量のスペースを使用する線形レイアウトを作成するには、各ビューの android:layout_height を「0dp」(垂直レイアウトの場合)に設定するか、各ビューの android:layout_width を「0dp」に設定します(水平レイアウトの場合)。次に、各ビューの android:layout_weight を「1」に設定します。

メニューを実装しようとしているので、最善の方法は、各 RelativeLayout (textview と imageview を使用) をボタンに置​​き換えることだと思います。したがって、レイアウトは次のようになります。

<?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"
android:weightSum="7.0" > //Defines the maximum weight sum


<Button 
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:height="0dp"
    android:layout_weight="1"
    android:text="Option 1"
    android:drawableLeft="@drawable/icon1"
    android:onClick="handleOption"/> // method to handle onClick

 <Button 
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:height="0dp"
    android:layout_weight="1"
    android:text="Option 2"
    android:drawableLeft="@drawable/icon2"
    android:onClick="handleOption"/> // method to handle onClick

    //Add more five buttons
    .
    .
    . 

あなたのアクティビティでは、このレイアウトをロードする必要があり、以下のようなsetContentView()メソッドを実装して各ボタンのイベントを処理する必要があります。handleOptiononClick

 public view handleOption(View view)
 {
     switch(view.getId()) ....
 }

この方法では、onClickListener インターフェースを実装する必要はなく、各ボタンに 1 つのメソッドを持ち、各ボタンに onClickListener を設定します。

于 2013-04-30T17:17:44.563 に答える