2017-12-07 11 views
-1

私はSQLiteを使って自分のプロジェクトのデータベースからバイログを表示しています。私はStringBufferのすべての行データを追加して、その後に改行を追加しています。しかし、私が望むのは、それぞれの改行が同じ位置から始まるということです。写真のようにブラックバーです。改行を追加した後に一番左にTextViewを整列させます

enter image description here

私はこれを行うための方法を見つけることができません。どんな助けもありがとう。ここで

は私のXMLコードです:

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="0dp" 
    android:orientation="vertical" 
    tools:layout_constraintTop_creator="1" 
    tools:layout_constraintRight_creator="1" 
    tools:layout_constraintBottom_creator="1" 
    android:layout_marginStart="8dp" 
    app:layout_constraintBottom_toBottomOf="parent" 
    android:layout_marginEnd="8dp" 
    app:layout_constraintRight_toRightOf="parent" 
    android:layout_marginTop="8dp" 
    tools:layout_constraintLeft_creator="1" 
    android:layout_marginBottom="8dp" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    android:gravity="center"> 

    <TextView 
     android:id="@+id/buyTv" 
     android:textSize="15sp" 
     android:textColor="#0000FF" 
     android:gravity="center" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:scrollbars = "vertical"/> 
</LinearLayout> 

し、このための私のJavaコード:

 StringBuffer buffer = new StringBuffer(); 

     while (cursor.moveToNext()) { 

      buffer.append("BuyID: " + cursor.getString(0) + "\n"); 
      buffer.append("Time: " + cursor.getString(1) + "\n"); 
      buffer.append("Product Name: " + cursor.getString(2) + "\n"); 
      buffer.append("Price: " + cursor.getString(3) + "\n"); 
      buffer.append("Quantity: " + cursor.getString(4) + "\n"); 
      buffer.append("Total Cost: " + cursor.getString(5) + "\n\n"); 
     } 

     String temp = buffer.toString(); 

     tv.setText(temp); 
     tv.setMovementMethod(new ScrollingMovementMethod()); 

答えて

0

変更するには、このようなstartまたはleftへのごのTextViewの重力:私は左の重力を使用している場合

<TextView 
    android:width="wrap_content" 
    android:width="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:id="@+id/buyTv" 
    android:textSize="15sp" 
    android:textColor="#0000FF" 
    android:gravity="left" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:scrollbars = "vertical"/> 
+0

左の重力を使用している場合、TextViewは非常に左の位置から整列しますが、同じ開始位置で中央に配置します。 – Rafat

+0

私の編集した回答を試してください –

+0

ありがとう!それだけが必要。 – Rafat

0

のTextView

android:gravity="center" 

または

にそれを修正するのプロパティを削除します
android:gravity="left" 
XMLファイルの

このプロパティは、テキストを中央に揃えます。

+0

TextViewには非常に左の位置から合わせるが、私は同じ開始位置とセンターでそれらをしたいです! – Rafat

+0

@Rafat TextViewの 'android:layout_width'プロパティを' WRAP_CONTENT'として修正し続けることができます。 –

+0

それは働いた!ありがとう。 – Rafat

関連する問題