[英]Set child layout width programmatically In ANDROID?
In my Code I have a XML file having multiple child layouts. 在我的代码中,我有一个具有多个子布局的XML文件。 Aand I just want to set one of these child layout's width programmatically according to device's screen width.
Aand我只想根据设备的屏幕宽度以编程方式设置其中一个子布局的宽度。 till now I had tried Layout params, setparam, and other methods but it shows null pointer exception on getting layout id.
到目前为止,我曾尝试过Layout params,setparam和其他方法,但它在获取布局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代码: -
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;
}
here is my code snippet and i comment the part what i tried so far and get any fine result..`` 这是我的代码片段,我评论我到目前为止尝试的部分,并得到任何好结果..``
Can you do this: Set the id to this child linear layout and then: 你能做到这一点:将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()
EDIT: 编辑:
I see you already tried this, sorry. 我看到你已经试过了,对不起。 But there are two linear layouts no ?
但是有两种线性布局没有?
Put this in java code: 把它放在java代码中:
int width = 300;
int height = LayoutParams.WRAP_CONTENT;
LinearLayout childLayout = (LinearLayout) findViewById(R.id.childLayout);
childLayout.setLayoutParams(new LayoutParams(width, height));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.