2

「One」、「Two」、「Three」、「Four」という要素を持つ文字列のリストがあるとします。

それらを画面の中央に配置したいのですが、各単語の開始が隣り合うようにしたいのです。

このような:

                                      One
                                      Two
                                      Three
                                      Four

フォントはおそらく文字ごとに長さが異なることを考えると、Android でこれをどのように行うのが最善でしょうか?

4

4 に答える 4

1

gravity中心にしたい要素と詳細のためにを設定できます

android:gravity="center_horizontal"
于 2010-07-10T16:37:09.067 に答える
1

I'm not sure how your application is behaving and if a ListView is strictly required, but the effect you desire can be achieved using TableLayout and TableRows. The TableLayout will line up the elements in each column for each row as you have described.

Having said that, TableLayout does not support having lines between the rows or gridlines (although I have seem some clever hacks involving changing the background colour of the TableRow to black, and then changing the padding and background colour of the View objects in the TableRow to white to get a black divider line - but that doesn't always work depending on your View objects).

I'm in a similar pickle, and that was the first avenue I examined. It didn't have a solution for my situation, but this might work for you. And if you do find a way of lining up the text in a List, I'd love to hear about it.

EDIT: I also feel it is worth mentioning (based on how the conversation is progressing) that you can also set row.setOnClickListener() and make an entire row clickable in a TableView. Once you wrap it in a ScrollView, it's pretty list-like.

于 2010-07-10T18:45:30.780 に答える
0

ListViewをlayout_gravity="center_horizo​​ntal"または

nvm:それは明らかに機能しません。

待ってください..各行が画面の幅全体でクリック可能で、すべての行を左揃えにし、両端揃えを壊さずにすべての両端揃えのテキストを中央揃えにするリストが必要ですか?

于 2010-07-11T11:09:38.243 に答える
0

layout_width="wrap_content"親全体を中心にして親内にアイテムを配置したいようです。

もしかして、こういうこと?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView android:text="Content above..."
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal">
        <TextView android:text="One"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView android:text="Two"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView android:text="Three"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <TextView android:text="Content below..."
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />    
</LinearLayout>

と の違いを覚えておいてgravityくださいlayout_gravity。このgravity属性は、ビューのコンテンツを参照します。layout_gravity(および で始まる他のすべての属性layout_) は、その親内のビューのレイアウトを参照します。

編集:layout_width="fill_parent" ListView アイテムを同様にフォーマットする場合は、 andを使用して ListView 自体を使用して、リスト アイテムのレイアウトとして次のようなものを試してくださいlayout_height="fill_parent"

<LinearLayout android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:weightSum="2"
    android:gravity="center">
    <TextView android:id="@+id/text" 
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

text通常の方法で、各リスト項目のid を持つ TextView の内容を変更します。現在のminHeightテーマから引き出された設定により、タッチに適したサイズが維持されます。

この場合の均一なセンタリングは、LinearLayout の とweightSumTextViewの の組み合わせによって処理されます。親の weightSum で割った TextView の重みは、LinearLayout が与える水平方向のスペースのパーセンテージを決定します。上記の例では、使用可能な水平スペースの 1/2 が取得されますが、中央に配置されます。gravitylayout_weight

ListView は現在画面に表示されていないリスト項目の内容を認識しないため、アダプタ内のすべての項目のテキストを測定して内容を完全に中央に配置する方法はありません。上記の例のようなリスト項目のレイアウトを使用して近似する必要があります。

于 2010-07-11T07:34:36.050 に答える