android - 在 ANDROID 中以编程方式设置子布局宽度?

标签 android android-layout android-widget

在我的代码中,我有一个包含多个子布局的 XML 文件。 Aand 我只想根据设备的屏幕宽度以编程方式设置这些子布局的宽度之一。 到目前为止,我已经尝试过 Layout params、setparam 和其他方法,但在获取布局 id 时显示空指针异常。

我的父布局是一个相对布局, 第一个子布局是线性布局(“对于这个布局,我需要改变宽度”)

<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 代码:-

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

这是我的代码 fragment ,我评论了到目前为止我尝试过的部分并得到了很好的结果..``

最佳答案

你可以这样做吗:将 id 设置为这个子线性布局,然后:

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

编辑:

我看到你已经试过了,抱歉。但是有两个线性布局不是吗?

关于android - 在 ANDROID 中以编程方式设置子布局宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13721090/

相关文章:

java - TypedArray getColor 总是返回 defValue

Android 布局 - 使某些对象可扩展

android - 我可以在测试项目中创建 Activity stub 吗?

android - 如何知道 Intent 是否在android中完成

Android相对布局重力填充问题

Android:ListView 中的自定义布局

android - 加速度计与重力传感器

android - 如何使用正确的用户代理显示 whatsapp

java - 如何在每次选择 Tab 时输出消息?

java - 更新 ArrayAdapter 中的数据