简体   繁体   English

以编程方式将规则添加到已在布局中的视图 - Android

[英]Adding Rule programatically to a View that is already in the layout - Android

How do i add a rule to a View that is already in my layout? 如何将规则添加到已在我的布局中的视图?

My XML: 我的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative_layout_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mylifeinformationsharedsocial.SexAcivity" >
<ListView
    android:id="@+id/list_view_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />    

</RelativeLayout>

I want to add layoutParams.addRule(RelativeLayout.BELOW,myView.getId()); 我想添加layoutParams.addRule(RelativeLayout.BELOW,myView.getId()); But if i add the View again to the layout it gives me an IllegalStateException(obviously Haha). 但是,如果我再次将View添加到布局中,它会给我一个IllegalStateException(显然是哈哈)。

So how do i do that? 那我该怎么做?

EDIT 编辑

Java: Java的:

listView = (ListView) findViewById(R.id.list_view_id);
listView.setAdapter(myArrayAdapter);

layoutParams.addRule(RelativeLayout.BELOW,datePicker.getId());
relativeLayout.addView(listView,layoutParams);

It throws the Exception at the addView() method 它在addView()方法中抛出异常

You can't add the view cause it's already added. 您无法添加视图,因为它已添加。 So, you need to set the correct layout params. 因此,您需要设置正确的布局参数。 You can either create new ones or retrieve the current ones and alter those. 您可以创建新的或检索当前的那些并更改它们。 Checkout this post . 看看这篇文章

layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.BELOW,myView.getId());
listView.setLayoutParams(layoutParams);

So as Matthias suggested: 正如马蒂亚斯所说:

layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.BELOW,myView.getId());
listView.setLayoutParams(layoutParams);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:如何在布局已经包含按钮的情况下以编程方式通过视图覆盖整个布局? - Android: How to cover the whole layout by a view programatically while the layout contains buttons already? Android以编程方式添加布局资源以进行查看 - Android programatically add layout resource to view 使用预定义的布局在Android中以编程方式添加视图 - Add view programatically in Android with a pre defined layout 如何以编程方式将自定义视图添加到布局 Android Studio - How do I add a custom view to a layout programatically Android Studio 以编程方式向Android Button添加保证金 - Programatically adding margin to Android Button Android-以编程方式设置“ android:layout_column =” 1””? - Android - setting the “android:layout_column=”1“” programatically? Android:添加/删除片段时视图布局重置 - Android: view layout resetting when adding/removing fragments Android 添加片段容器视图后膨胀布局崩溃 - Android inflate layout crashes after adding fragment container view 在Android中以编程方式设置EditText的layout_width - Programatically set layout_width of EditText in Android 如何以编程方式将 android 视图连接到 XML 视图? - How to connect android view to XML view programatically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM