Java – how do I align these numbers with decimals in Android?
•
Android
My activity fullscreen.xml has this excerpt, which works exactly as I want to produce decimal aligned output:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal|top"
android:gravity="center">
<TextView
android:layout_width="140dp"
android:layout_height="24dp"
android:text="-0.123"
android:id="@+id/acc_x"
android:layout_gravity="center_horizontal|top"
android:textColor="#ffffff"
android:gravity="center_vertical|end"
android:layout_margin="0dp"
style="@style/Base.TextAppearance.AppCompat.Large" />
<TextView
android:layout_width="140dp"
android:layout_height="24dp"
android:text="0.123"
android:id="@+id/acc_y"
android:layout_gravity="center_horizontal|top"
android:textColor="#ffffff"
android:gravity="center_vertical|end"
android:layout_margin="0dp"
style="@style/Base.TextAppearance.AppCompat.Large" />
<TextView
android:layout_width="140dp"
android:layout_height="24dp"
android:text="99.123"
android:id="@+id/acc_z"
android:layout_gravity="center_horizontal|top"
android:textColor="#ffffff"
android:gravity="center_vertical|end"
android:layout_margin="0dp"
style="@style/Base.TextAppearance.AppCompat.Large" />
<TextView
android:layout_width="140dp"
android:layout_height="24dp"
android:text="99.123"
android:id="@+id/acc_T"
android:layout_gravity="center_horizontal|top"
android:textColor="#ffffff"
android:gravity="center_vertical|end"
android:layout_margin="0dp"
style="@style/Base.TextAppearance.AppCompat.Large" />
</LinearLayout>
However, when I use this code to calculate numbers and fill the same text box, the alignment changes:
x = -0.123f;
text = (TextView)findViewById(R.id.acc_x);
text.setText(String.format("%-8.3f",x));
y = 0.123f;
text = (TextView)findViewById(R.id.acc_y);
text.setText(String.format("%-8.3f",y));
z = 99.123f;
text = (TextView)findViewById(R.id.acc_z);
text.setText(String.format("%-8.3f",z));
T = 0;
text = (TextView)findViewById(R.id.acc_T);
text.setText(String.format("%-8.3f",T));
What can I do to keep the decimal point aligned?
This is my build.gradle. If it's important:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.app001"
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:support-v4:21.0.3'
}
resolvent:
You don't need to change anything in layout.xml. I ran this code, changed the string format, and the problem disappeared
x = -0.123f;
text = (TextView)findViewById(R.id.acc_x);
text.setText(String.format("%.3f",x));
y = 0.123f;
text = (TextView)findViewById(R.id.acc_y);
text.setText(String.format("%.3f",y));
z = 99.123f;
text = (TextView)findViewById(R.id.acc_z);
text.setText(String.format("%.3f",z));
T = 0;
text = (TextView)findViewById(R.id.acc_T);
text.setText(String.format("%.3f",T));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码