2013-03-19 89 views
13

您好,我有一個主屏幕。以編程方式添加帶參數的佈局Android

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity" > 
<fragment 
    android:id="@+id/map" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    class="com.google.android.gms.maps.SupportMapFragment"/> 
<ImageButton 
    android:id="@+id/imageButton1" 
    android:layout_width="55dp" 
    android:layout_height="50dp" 
    android:layout_alignParentTop="true" 
    android:layout_marginTop="10dp" 
    android:layout_toLeftOf="@+id/toggleButton1" 
    android:background="@android:drawable/btn_default" 
    android:contentDescription="@string/app_name" 
    android:scaleType="centerCrop" 
    android:src="@drawable/plus_grey" /> 

<LinearLayout 
    android:id="@+id/lay" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_marginEnd="0dp" 
    android:background="#A0FFFFFF" 
    android:orientation="horizontal" 
    android:visibility="invisible" > 

    <TextView 
     android:id="@+id/locinfo" 
     android:layout_width="match_parent" 
     android:layout_height="60dp" 
     android:textColor="@color/cherniy" 
     android:textSize="20sp" 
     android:visibility="visible" /> 
</LinearLayout> 
</RelativeLayout> 

而且還有一個ImageButton的

    button = (ImageButton) findViewById(R.id.imageButton1); 
     button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       if (bIcon == false) { 
        button.setImageResource(R.drawable.plus_yellow);                 
        m = myMap.addMarker(new MarkerOptions().position(myMap.getCameraPosition().target).draggable(true));      
        LinearLayout linLayout = (LinearLayout)findViewById(R.id.lay); 
        t = (TextView)findViewById(R.id.locinfo);      
        linLayout.setVisibility(View.VISIBLE); 
        t.setVisibility(View.VISIBLE);      
           bIcon = true; 
       } 
       else { 
        button.setImageResource(R.drawable.plus_grey);            
        m.remove(); 
        LinearLayout linLayout = (LinearLayout)findViewById(R.id.lay); 
        t.setVisibility(View.INVISIBLE); 
        linLayout.setVisibility(View.INVISIBLE);       
        bIcon = false;     
       }        
      } 
     }); 

我想以編程方式添加

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((w*2)/3, LayoutParams.WRAP_CONTENT); 

其中w

display = getWindowManager().getDefaultDisplay(); 
@SuppressWarnings("deprecation") 
w = display.getWidth(); 

但是當我做這樣的

   button.setImageResource(R.drawable.plus_yellow);                 
       m = myMap.addMarker(new MarkerOptions().position(myMap.getCameraPosition().target).draggable(true));      
       LinearLayout linLayout = (LinearLayout)findViewById(R.id.lay); 
       LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((w*2)/3, LayoutParams.WRAP_CONTENT); 
       linLayout.setLayoutParams(lp); 
       t = (TextView)findViewById(R.id.locinfo);      
       linLayout.setVisibility(View.VISIBLE); 
       t.setVisibility(View.VISIBLE);      
       bIcon = true; 

我的應用程序崩潰。你能告訴我如何以編程方式創建我的參數(與設備的屏幕寬度和高度綁定)?我會採取任何建議。謝謝。

回答

17
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
    (w*2)/3, 
    LayoutParams.WRAP_CONTENT 
); 
linLayout.setLayoutParams(lp); 

這將引發錯誤,因爲LinearLayout是相對佈局的孩子,所以你必須設置RelativeLayoutParams了點。

而是使用

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
    (w*2)/3, 
    RelativeLayout.LayoutParams.WRAP_CONTENT 
); 
linLayout.setLayoutParams(lp); 

或 而不是創造新的LayoutParams可以重用針對存在的LayoutParams如下表所示。

RelativeLayout.LayoutParams lp = linLayout.getLayoutParams(); 
lp.width = (w*2)/3; 
lp.height = RelativeLayout.LayoutParams.WRAP_CONTENT; 
linLayout.setLayoutParams(lp); 
+0

謝謝。你需要什麼 – 2013-03-19 18:50:58

+0

你可以把進口?我知道是「viewgroup」,但請把= :) – delive 2015-10-01 12:19:42

0

而不是製作一個新的LayoutParams,你應該修改現有的。

你的代碼更改爲:

button.setImageResource(R.drawable.plus_yellow);                 
m = myMap.addMarker(new MarkerOptions().position(myMap.getCameraPosition().target).draggable(true));      
LinearLayout linLayout = (LinearLayout)findViewById(R.id.lay); 
LinearLayout.LayoutParams lp = linLayout.getLayoutParams(); 
final int left_margin = whatever; 
final int top_margin = 0; 
final int right_margin = whatevermore; 
final int bottom_margin = 0; 
lp.setMargins(left_margin, top_margin, right_margin, bottom_margin); 
lp.width = (w*2)/3; 
linLayout.setLayoutParams(lp); 
t = (TextView)findViewById(R.id.locinfo);      
linLayout.setVisibility(View.VISIBLE); 
t.setVisibility(View.VISIBLE);    
bIcon = true; 
相關問題