在Android中以编程方式添加带参数的布局

15

你好,我有一个主屏幕。

<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;

我的应用程序崩溃了。请告诉我如何以编程方式使用我的参数创建应用程序(与设备屏幕的宽度和高度相关)?我将接受任何建议。谢谢。

我的意见是,您可以在代码中使用屏幕宽度和高度的变量,然后根据这些变量来设置您的参数。如果您需要更具体的帮助,请提供更多细节或代码示例。
2个回答

20
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);

你能加入导入吗?我知道是“viewgroup”,但请加上“=” :) - user3402040

0

不要创建新的LayoutParams,而是修改现有的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;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接