添加布局参数编程机器人 [英] Add layout with parameters programmatically Android

查看:101
本文介绍了添加布局参数编程机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个主屏幕。

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=MainActivity。>
<片段
    机器人:ID =@ + ID /图
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    类=com.google.android.gms.maps.SupportMapFragment/>
<的ImageButton
    机器人:ID =@ + ID / imageButton1
    机器人:layout_width =55dp
    机器人:layout_height =50dp
    机器人:layout_alignParentTop =真
    机器人:layout_marginTop =10dp
    机器人:layout_toLeftOf =@ + ID / toggleButton1
    机器人:背景=@机器人:可绘制/ btn_default
    机器人:contentDescription =@字符串/ APP_NAME
    机器人:scaleType =centerCrop
    机器人:SRC =@可绘制/ plus_grey/>

<的LinearLayout
    机器人:ID =@ + ID /打好
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentBottom =真
    机器人:layout_alignParentLeft =真
    机器人:layout_alignParentRight =真
    机器人:layout_marginEnd =0dp
    机器人:后台=#A0FFFFFF
    机器人:方向=横向
    机器人:能见度=隐形>

    <的TextView
        机器人:ID =@ + ID / locinfo
        机器人:layout_width =match_parent
        机器人:layout_height =60dp
        机器人:文字颜色=@色/ cherniy
        机器人:TEXTSIZE =20SP
        机器人:能见度=看见/>
< / LinearLayout中>
< / RelativeLayout的>
 

和存在的ImageButton

 按钮=(的ImageButton)findViewById(R.id.imageButton1);
        button.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(视图v){
                如果(Bicon的== FALSE){
                    button.setImageResource(R.drawable.plus_yellow);
                    M = myMap.addMarker(。新MarkerOptions()位置(myMap.getCameraPosition()目标).draggable(真));
                    的LinearLayout linLayout =(的LinearLayout)findViewById(R.id.lay);
                    T =(TextView中)findViewById(R.id.locinfo);
                    linLayout.setVisibility(View.VISIBLE);
                    t.se​​tVisibility(View.VISIBLE);
                                Bicon的=真;
                }
                其他 {
                    button.setImageResource(R.drawable.plus_grey);
                    m.remove();
                    的LinearLayout linLayout =(的LinearLayout)findViewById(R.id.lay);
                    t.se​​tVisibility(View.INVISIBLE);
                    linLayout.setVisibility(View.INVISIBLE);
                    必康= FALSE;
                }
            }
        });
 

我想以编程方式添加

  LinearLayout.LayoutParams LP =新LinearLayout.LayoutParams((W * 2)/ 3,LayoutParams.WRAP_CONTENT);
 

其中w

 显示= getWindowManager()getDefaultDisplay()。
@燮pressWarnings(德precation)
瓦特= display.getWidth();
 

但是,当我不喜欢这样

  button.setImageResource(R.drawable.plus_yellow);
                M = myMap.addMarker(。新MarkerOptions()位置(myMap.getCameraPosition()目标).draggable(真));
                的LinearLayout linLayout =(的LinearLayout)findViewById(R.id.lay);
                LinearLayout.LayoutParams LP =新LinearLayout.LayoutParams((W * 2)/ 3,LayoutParams.WRAP_CONTENT);
                linLayout.setLayoutParams(LP);
                T =(TextView中)findViewById(R.id.locinfo);
                linLayout.setVisibility(View.VISIBLE);
                t.se​​tVisibility(View.VISIBLE);
                Bicon的=真;
 

我的应用程序崩溃。你能告诉如何以编程方式与我的参数(并列的宽度和设备屏幕的高度)创造?我会采取任何建议。谢谢你。

解决方案

  LinearLayout.LayoutParams LP =新LinearLayout.LayoutParams(
    (瓦特* 2)/ 3,
    LayoutParams.WRAP_CONTENT
);
linLayout.setLayoutParams(LP);
 

这将引发错误,因为的LinearLayout 是相对布局的孩子,所以你必须设置 RelativeLayoutParams 的这一点。

而不是使用

  RelativeLayout.LayoutParams LP =新RelativeLayout.LayoutParams(
    (瓦特* 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);
 

Hello I have a main screen.

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

And there is a 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;                
                }                             
            }
        });

I want to programmatically add

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

where w

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

But when I do like this

                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;

My application crashes. Can you please tell how to programmatically create with my parameters (tied with the width and height of the screen of the device)? I will take any suggestions. Thank you.

解决方案

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

This will throw error because the LinearLayout is the child of Relative layout, so you have to set RelativeLayoutParams for that.

Instead use this

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

or Instead of creating new LayoutParams you can reuse the existing LayoutParams of the view as shown below.

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

这篇关于添加布局参数编程机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆