我试图以编程方式将视图添加到线性布局.
LinearLayout layout = (LinearLayout) findViewById(R.id.info);
String [] informations = topOffer.getInformations();
TextView informationView;
View line = new View(this);
line.setLayoutParams(new LayoutParams(1, LayoutParams.FILL_PARENT));
line.setBackgroundColor(R.color.solid_history_grey);
for (int i = 0; i < informations.length; i++) {
informationView = new TextView(this);
informationView.setText(informations[i]);
layout.addView(informationView, 0);
layout.addView(line, 1);
}
Run Code Online (Sandbox Code Playgroud)
首先,我只添加了informationsView,一切正常.在添加了line-View之后,它崩溃了,出现以下错误:
java.lang.IllegalStateException:指定的子级已有父级.您必须首先在孩子的父母上调用removeView().
所以我尝试了addView(View v,int index),但它崩溃了同样的消息......
有人有解决方案吗?
谢谢,马丁
我试图创建一个定制的Android控制是包含一个LinearLayout中.您可以将其视为具有花式边框,背景,左侧图像的扩展LinearLayout ...
我可以用XML完成所有工作(效果很好),但由于我的应用程序中有很多次出现,因此很难维护.我觉得这样的东西会更好:
/* Main.xml */
<MyFancyLayout>
<TextView /> /* what goes inside my control's linear layout */
</MyfancyLayout>
Run Code Online (Sandbox Code Playgroud)
你会怎么做?我想避免在onMeasure/onLayout方法上重写整个线性布局.这就是我现在所拥有的:
/* MyFancyLayout.xml */
<TableLayout>
<ImageView />
<LinearLayout id="container" /> /* where I want the real content to go */
</TableLayout>
Run Code Online (Sandbox Code Playgroud)
和
/* MyFancyLayout.java */
public class MyFancyLayout extends LinearLayout
{
public MyFancyLayout(Context context) {
super(context);
View.inflate(context, R.layout.my_fancy_layout, this);
}
}
Run Code Online (Sandbox Code Playgroud)
您如何在正确的位置(id =容器)插入用户指定的内容(main.xml中的TextView)?
干杯!
罗曼
-----编辑-------
仍然没有运气,所以我改变了我的设计,使用更简单的布局,并决定使用一些重复的XML.仍然非常感兴趣的人知道如何做到这一点!
可以在向上方向上一个接一个地向线性布局添加视图.
如果有人正在研究那个,请帮助我
我有一个LinearLayout设置高度为match_parent,如下所示:
<LinearLayout
android:id="@+id/list_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
Run Code Online (Sandbox Code Playgroud)
我想得到这个LinearLayout的高度.
我使用下面的代码:
LinearLayout ll_list = (LinearLayout)findViewById(R.id.list_layout);
int h = ll_list.getHeight();
Run Code Online (Sandbox Code Playgroud)
但它返回null.
我能怎么做?
我遇到了包含一个包含LinearLayout的ScrollView的Fragment问题.我正在尝试创建一种效果,其中LinearLayout具有白色背景,看起来像一张纸在彩色背景上滚动.我试图实现这一目标的方法是让ScrollView占据片段的整个空间,然后内部的LinearLayout必须android:layout_margin="16dp"创建"纸张"周围的空间.
这样,ScrollView的滚动条显示在彩色背景区域中,顶部的边距与内容一起滚动,底部的边距仅在到达末尾时滚动.
不幸的是,在这种配置中,ScrollView不会一直滚动到最后,实际上会截断底部的非常少量的文本.我怀疑ScrollView没有在其垂直滚动距离中考虑其孩子的边距.为了解决这个问题,我将LinearLayout包装在一个解决问题的FrameLayout中,但似乎是多余的.关于如何消除这个不需要的容器的任何指示将不胜感激.
注意:android:padding="16dp"在ScrollView上设置并删除边距不会产生所需的效果,因为无论滚动位置如何,填充都会连续显示在所有四个边上.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
tools:context=".ArticleFragment" >
<!-- This FrameLayout exists purely to force the outer ScrollView to respect
the margins of the LinearLayout -->
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:layout_margin="16dp"
android:background="@color/page_background" >
<TextView
android:id="@+id/article_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textIsSelectable="true" />
<TextView
android:id="@+id/article_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textIsSelectable="true" />
</LinearLayout>
</FrameLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud) android android-layout android-linearlayout android-scrollview
我正在创建包含a的动态视图LinearLayout并将它们添加到外部LinearLayout.我想在创建的视图周围设置边距,但layout_margin忽略XML文件中的边距.如果我在代码中设置参数,它可以工作,但我想在布局XML中指定边距.
设置XML布局中的边距将被忽略:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical" >
...
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
在创建时设置边距是值得尊重的:
LinearLayout productView = (LinearLayout) getLayoutInflater().inflate(R.layout.product_preview, null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.setMargins(50, 50, 50, 50);
productView.setLayoutParams(params);
Run Code Online (Sandbox Code Playgroud)
这是外部布局.视图已添加到dealer_activity_product_list.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/dealer_activity_dealer_image"
android:layout_width="match_parent"
android:layout_height="150dp"
android:contentDescription="@string/dealer_activity_dealer_image_desc" />
<TextView
android:id="@+id/dealer_activity_dealer_address"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/dealer_activity_product_list1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" />
<LinearLayout
android:id="@+id/dealer_activity_product_list2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" />
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我正在游戏中实现自定义可扩展操作栏.我正在使用ViewDragHelper来处理条形视图的拖动和拖动,它包含在LinearLayoutI子类中以附加ViewDragHelper.
以下链接对实现此目的有很大帮助:
我遇到的唯一问题是我的父LinearLayout 的layout()的行为:在每次调用layout()/时onLayout(),子draggable/expandable操作栏被重置为其原始位置(XML布局中设置的位置).
这是为什么 ?
(根据我的经验,layout()从未对已被移动的观点的位置感到困惑)
我在CardView中使用了一个LinearLayout来获得一种社交媒体登录按钮外观,它运行良好,但Android Studio标记Element LinearLayout is not allowed here.我想知道为什么会这样呢?我的xml是这样的:
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_below="@id/cv_fb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="3dp"
>
<LinearLayout
android:id="@+id/ll_entitlement_extend_google"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@color/white"
android:clickable="true"
android:gravity="center"
android:paddingTop="10dp"
android:paddingBottom="10dp"
>
<ImageView
android:layout_weight="1"
android:layout_width="75dp"
android:layout_height="75dp"
android:src="@drawable/google_icon"/>
<TextView
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connect with Google +"/>
<ImageView
android:layout_weight="1"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/ic_chevron_right_black"/>
</LinearLayout>
</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud) android android-layout android-linearlayout android-cardview
我有一个布局,用于更新状态栏的背景颜色colorPrimaryDark.
当布局的根布局为a时CoordinatorLayout,这很有用,但当我将其切换到LinearLayout状态栏时,背景不再更新.
布局源和屏幕截图粘贴在下面.还列出了正常工作的布局示例.
谢谢!
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".churches.ChurchesActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="@+id/contentFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
<style name="AppTheme.PopupOverlay" …Run Code Online (Sandbox Code Playgroud) android android-layout android-linearlayout android-notification-bar android-coordinatorlayout
我正在构建一个MainActivity包含两个片段的Activity的应用程序.我遇到了很多教程,它们FrameLayout用作片段布局的父容器.
但是,我开始知道我仍然可以使用LinearLayout片段的父容器,它完全正常(到目前为止).
我的问题是,是否存在不使用FrameLayout片段布局的父容器的副作用?
如果没有,那么使用FrameLayoutover LinearLayout(或其他可能的布局)作为片段布局的父容器有什么好处.
android android-layout android-linearlayout android-framelayout