设置子布局的宽度以编程方式ANDROID? [英] Set child layout width programmatically In ANDROID?

查看:149
本文介绍了设置子布局的宽度以编程方式ANDROID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的code我已经有多个孩子一个布局的XML文件。 A和我只是想以编程根据设备的屏幕宽度来设置这些子布局的宽度之一。
到现在我都试过布局参数,可以setparam等方法,但它显示了越来越布局ID空指针异常。

In my Code I have a XML file having multiple child layouts. Aand I just want to set one of these child layout's width programmatically according to device's screen width. till now I had tried Layout params, setparam, and other methods but it shows null pointer exception on getting layout id.

我的父母布局是一个相对布局,
和第一子布局是一个线性布局(此布局我需要改变宽度)

my parent layout is a Relative Layout, and 1st Child Layout is a Linear Layout("for this layout I need to change width")

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parentlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:id="@+id/childlayout"
        android:layout_width="250dip"
        android:layout_height="fill_parent"
        android:orientation="vertical" 
        android:background="#153746">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@drawable/sub_title"
            android:gravity="center_vertical" >
            <TextView
                android:id="@+id/menu_header_label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:text="asd"
                android:textColor="#ffffff"
                android:layout_marginLeft="10dip" />

            <ImageView
                android:id="@+id/menu_header"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal" />
        </LinearLayout>
        <ListView
            android:id="@+id/menu_listview"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:divider="@android:color/darker_gray"
            android:dividerHeight="1dip"
            android:scrollingCache="false" />
    </LinearLayout>

    <FrameLayout
        android:id="@+id/overlay"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </FrameLayout>

</LinearLayout>

Java的code: -

Java Code:-

     public class SlideMenu extends LinearLayout {

    // keys for saving/restoring instance state
    public final static String KEY_MENUSHOWN = "menuWasShown";
    private final static String KEY_STATUSBARHEIGHT = "statusBarHeight";
    private final static String KEY_SUPERSTATE = "superState";

//  Display display = ((WindowManager) getWindowToken()).getDefaultDisplay(); 
//  int width = display.getWidth();  // deprecated
//  int height = display.getHeight();  // deprecated
//  float layWidth = (width*70)/100;
//  
////    LinearLayout view= (LinearLayout)findViewById(R.id.childlayout);
////    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
////    params.width = 130;
////    view.setLayoutParams(params);
//  view.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, theSizeIWant));

    public static class SlideMenuItem {
        public int id;
        public Drawable icon;
        public String label;
    } 

这是我的code段,我评论的部分是我的尝试,到目前为止,并得到任何罚款result..``

here is my code snippet and i comment the part what i tried so far and get any fine result..``

推荐答案

你可以这样做:将ID为这个孩子的线性布局,然后:

Can you do this: Set the id to this child linear layout and then:

LinearLayout linLayout = (LinearLayout) findViewById(R.id.yourID);
LinearLayout .LayoutParams layoutParams= new 
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
LinearLayout.LayoutParams.MATCH_PARENT);//or whatever , you can set the width and height programmatically and then setLayoutParams()

编辑:

我看你已经尝试过这一点,不好意思。但有两个线性布局,不是吗?

I see you already tried this, sorry. But there are two linear layouts no ?

这篇关于设置子布局的宽度以编程方式ANDROID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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