2013-04-23 7 views
4

LayoutListView의 텍스트 항목을 가로로 가운데 정렬 할 수 있습니까?
솔직히, 나는 그런 기본적인 질문을하기 전에 적어도 한 시간 씩 인터넷 검색을했습니다.ListView의 가운데 텍스트 항목

감사합니다.

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

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:hint="Remind..." /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:baselineAligned="false" 
     android:orientation="horizontal" > 

     <ListView 
      android:id="@+id/listviewTimeInterval" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:scrollbars="none" /> 

     <-- . . . --/> 
     <-- more lists of the same kind--/> 
     <-- . . .--/> 

    </LinearLayout> 

</LinearLayout> 
+0

'TextViews'의 '중력'을 사용자 정의 항목 XML에 설정하면 안됩니까? –

답변

13

당신은 당신의 목록보기 항목에 대한 자신의 레이아웃을 만들 필요가,이

예 같은 :

textcenter.xml :

<LinearLayout 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:gravity="center" > 
    <TextView 
     android:id="@id/textItem" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

그런 다음 코드에서, 당신의 이것을해야만한다.

ArrayAdapter<String> ad = new ArrayAdapter<String>(this, R.layout.textcenter, R.id.textItem, functions); 
listView.setAdapter(ad); 
+1

귀하의 답변이 유효한 경우. 하지만 OP가 이미 일종의 사용자 정의'ListView '를 사용하고 있다고 생각합니다. ID가'listviewTimeInterval'입니다 –

+3

ListView 자체가 텍스트가 어떻게 나타나는지 제어하지 않습니까? 별도의 레이아웃을 사용해야하는 이유는 무엇입니까? – jackhab

+0

글쎄, 나는 그들이 단지 활동에 필요하다고 생각하는 레이아웃의 역할을 오해했다. 그렇다면 사실 ListView는 특정 레이아웃을 가진 여러 뷰의 목록이라는 것을 깨달았습니다. – jackhab

-4

사용자 지정된 ListView xml 파일의 TextView 레이아웃에 android:gravity="center"을 사용하십시오. 그럴거야.

+0

이것은 약간 짧은 답변이며 주석이 될 수 있습니다. StackOverflow에서 질문에 답변하는 방법에 대한 [가이드] (http://stackoverflow.com/help/how-to-answer)를 읽었습니까? – starsplusplus

3

은 내가 LisView 자체가 안드로이드로 정의 된 경우는 불가능있는 ListView의 항목을 중심 발견 : layout_width = "wrap_content" 이 sence가되지 않습니다,이 매개 변수 만의 ListView의 위치에 영향을 안하기 때문에 그 요소들.

어쨌든 목록의 너비를 match_parent로 변경하면 항목을 중력 속성 (layout_)으로 가운데 놓을 수 있습니다.

+0

고마워요! 몇 시간 동안 listview를 중앙에 배치하려고했습니다. –