Porównaj ceny domen i usług IT, sprzedawców z całego świata

Ustawienie szerokości widoku tekstu na LinearLayout


Używam tytułu dla widoku listy. Nagłówek ListView ma trzy kolumny. Powiedz a, b, c. Używam dwóch LinearLayouts do zaprojektowania nagłówka ListView, jak pokazano poniżej:
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="[url=http://schemas.android.com/apk/res/android"]http://schemas.android.com/apk/res/android"[/url]
android:layout_width="fill_parent"
android:layout_height="40dip"
android:padding="0dip">
<LinearLayout
android:orientation="horizontal"
android:background="#1e90ff"
android:layout_width="0dip"
android:layout_weight="1"
android:padding="5dip"
android:layout_height="40dip">
<TextView
android:id="@+id/a"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#000000"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="@string/a"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#000000"
android:layout_weight="1"
android:gravity="center_vertical"
android:id="@+id/b"
android:singleLine="true"
android:text="@string/b"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#000000"
android:layout_weight="1"
android:gravity="center_vertical"
android:id="@+id/c"
android:singleLine="true"
android:text="@string/c"
/>
</LinearLayout>
</LinearLayout>

Teraz chcę poprawić szerokość kolumn odpowiednio a, b, c. Jak ustawić szerokość tych kolumn? Ponownie, czy dobrze jest używać do tego celu LinearLayout? Proszę doradź.
Zaproszony:
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Aby zmienić layout_width widoków tekstu na stałą szerokość, użyj:
android:layout_width="40dip"

A może chcesz, aby zajmowały wartości procentowe ekranu, zmieniając layout_weight
android:layout_weight="2"

W systemie Android waga wszystkich elementów w innym (tak jak tutaj układ liniowy) zostanie zsumowana, a następnie na podstawie wagi każdego widoku określ, ile miejsca zajmuje. TO ZNACZY. jeśli masz odpowiednio wagę 2,3,5, b, c, będzie ona miała szerokość 20%, szerokość 30% i szerokość c 50%.
Jeśli potrzebujesz kolumn, powinieneś rozważyć użycie układu tabeli (

podręcznik
http://developer.android.com/g ... .html
)
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

możesz również ustawić szerokość jako wartość procentową w ten sposób
<LinearLayout 
android:orientation="horizontal"
android:background="#1e90ff"
android:layout_width="0dip"
android:layout_weight="1"
android:padding="5dip"
android:layout_height="40dip"> <!--
android:layout_weight="0.5" <= it will set the 50 % width of screen
-->
<TextView
android:id="@+id/a"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#000000"
android:layout_weight="0.5"
android:gravity="center_vertical"
android:text="@string/a"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#000000"
android:layout_weight="0.5"
android:gravity="center_vertical"
android:id="@+id/b"
android:singleLine="true"
android:text="@string/b"
/>
</LinearLayout>
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Możesz użyć metody
setWidth ()
TextView
, aby dynamicznie ustawić rozmiar
Anonimowy użytkownik

Anonimowy użytkownik

Potwierdzenie od:

Jeśli robisz to w układzie, który jest używany przez ListView, wtedy działa tylko dynamiczne dostosowywanie szerokości w kodzie. Właściwie na mojej liście mam trzy widoki TextViews, ale pokazuję tutaj tylko dwa dla zwięzłości. Oto XML z układu:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[url=http://schemas.android.com/apk/res/android"]http://schemas.android.com/apk/res/android"[/url]
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="100"> <TextView
android:id="@+id/sl_name"
android:layout_width="wrap_content"
android:layout_weight="40"
android:layout_height="wrap_content"
android:textSize="16sp"/> <TextView
android:id="@+id/sl_score"
android:textSize="16sp"
android:gravity="right"
android:paddingRight="8dp"
android:layout_width="wrap_content"
android:layout_weight="15"
android:layout_height="wrap_content"/>
</LinearLayout>

Oto kod z adaptera dla ListView:
public View getView(int position, View cvtView, ViewGroup parent)
{
int width = parent.getWidth();
View rowView = inflater.inflate(R.layout.scorelayout, parent, false);
String str = values.get(position);
String[] vals = str.split("\t"); TextView tvName = (TextView)rowView.findViewById(R.id.sl_name);
tvName.setWidth((int)(width *.30));
tvName.setText(vals[2]); TextView tvScore = (TextView)rowView.findViewById(R.id.sl_score);
int stemp = Integer.parseInt(vals[0]);
tvScore.setWidth((int)(width * .15));
tvScore.setText(Integer.toString(stemp));
}

Aby odpowiedzieć na pytania, Zaloguj się lub Zarejestruj się