2012-12-29 22 views
5

mam ten układ xml do widoku listy:zmienić szerokość układu liniowego w android

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/menu" 
android:orientation="vertical" android:background="#2f4f4f" android:layout_width="wrap_content" android:layout_height="fill_parent"> 

    <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" 
    android:background="#2f4f4f" android:cacheColorHint="#2f4f4f" android:scrollbars="none"> 
    </ListView> 
</LinearLayout> 

Układ liniowy zajmuje całą szerokość ekranu domyślnie.

enter image description here

Chcę zmienić programatically.I to chcesz zrobić coś takiego:

enter image description here

Ale kiedy zmienić szerokość linearlayout nic się nie dzieje, to nadal zajmuje pełną szerokość ekranu

LayoutInflater inflater = LayoutInflater.from(this); 
View menu = inflater.inflate(R.layout.xml_layout, null); 
menu.setLayoutParams(new LayoutParams(20,LayoutParams.WRAP_CONTENT)); 

po wyświetleniu szerokości układu hardcoding w widoku listy jako:

<ListView 
    android:id="@+id/list" 
    android:layout_width="200dp" 
    android:layout_height="wrap_content" 
    android:background="#2f4f4f" 
    android:cacheColorHint="#2f4f4f" 
    android:scrollbars="none" > 

</ListView> 

uzyskać to:

enter image description here

Proszę zaproponować sposób, aby to zrobić.

Odpowiedz

4

dokonać zmian w widoku listy, jak pokazano poniżej, zrobić twardy kod dla szerokości układu w XML.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" android:background="@android:color/white"> 


<ListView 
    android:id="@+id/list" 
    android:layout_width="200dp" 
    android:layout_height="wrap_content" 
    android:background="#2f4f4f" > 

</ListView> 

</LinearLayout> 

robiłeś Wrap_content w układzie liniowym aw listview fill_parent, dlatego linearlayout który jest owijanie listview czyli pełny ekran.

+0

Po wyświetleniu szerokości układu hardcoding dostaję coś innego, zobacz moje edytowane pytanie. Również chcę zmienić szerokość menu na podstawie preferencji, więc muszę to zrobić programowo. – vishesh

+0

zmienić kolor tła układu liniowego na biały –

+0

zaktualizowałam moją odpowiedź –

2

Jeśli chcesz, aby pasował do wielu rozmiarów ekranu, sugerowałbym dodanie pustego układu i użycie odważników.

Jak to:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" android:background="@android:color/white"> 


<ListView 
    android:id="@+id/list" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="3" 
    android:background="#2f4f4f" > 

</ListView> 

<LinearLayout android:layout_width="0dp" 
android:layout_height="fill_parent" 
android:layout_weight="1" 
android:orientation="horizontal" android:background="@android:color/white"> 

</LinearLayout> 

Właśnie eyeballed wagi. Dostosuj je do szerokości, której potrzebujesz. Jeśli zrobisz to w ten sposób, nie będziesz musiał twardo kodować rozmiaru dla każdego rozmiaru ekranu.

Powiązane problemy