Reputation: 2325
There are 3 strings, each the length of string is the same, there are spaces in the those strings. After I fill the 3 strings in a ListView control, I find the width of the 3 row isn't the same.
How can I make the width of string the same in Android? Thanks!
BTW, I use Android Studio. The min API is 9.
string1= "a b"
string2= "aa b"
string3= "aaa b"
ListView lv=(ListView)findViewById(R.id.listView);
String[] COUNTRIES=new String[]{"a b","aa b","aaa b"};
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, COUNTRIES));
Upvotes: 0
Views: 481
Reputation: 644
add the views programmatically to the linear layout i have done in one of my program
xml (no of linearlayout as rows)
<LinearLayout
android:id="@+id/ansrow2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:gravity="center"
android:horizontalSpacing="4dp"
android:numColumns="10"
android:orientation="horizontal"
android:paddingEnd="10dp"
android:paddingStart="10dp"
android:verticalSpacing="10dp" >
</LinearLayout>
for (int i = 0; i < list2.size(); i++) {
text = new TextView(MainActivity.this);
text.setBackgroundColor(Color.WHITE);
text.setGravity(Gravity.CENTER);
text.setText("" + list2.get(i).toString().toLowerCase());
text.setPadding(10, 10, 10, 10);
text.setTextSize(35f);
text.setTypeface(rail);
text.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
5f));
text.setId(101 + i);
if (i < 10) {
ansrow2.addView(text);
} else {
ansrow1.addView(text);
}
text.setOnClickListener(this);
}
try to understand the code and implement it according to your needs
Upvotes: 0
Reputation: 4292
Unless you're using a monospaced font, achieving what you wanted is going to be quite difficult. Non-monospaced fonts has different width for each of the characters they posses.
However, there's a workaround to this using layout arrangement. Here's an example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="300dp"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.7">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="A"
android:id="@+id/textView2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="AAA"
android:id="@+id/textView4"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="AAAAA"
android:id="@+id/textView5"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="B"
android:id="@+id/textView3"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="BBB"
android:id="@+id/textView6"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="BBBB"
android:id="@+id/textView7"/>
</LinearLayout>
</LinearLayout>
The above code will produce a layout like this:
Upvotes: 2