- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 android 应用程序开发的新手,我遇到了一个问题:
当按下我的 DrawerLayout 上的按钮时,我正在向我的主 LinearLayout 添加内容:
private void switchTo(String text) {
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0.0F);
final TextView textView = new TextView(this);
textView.setLayoutParams(params);
textView.setText("New text: " + text);
content.addView(textView, 0);
}
但这会将文本添加到我的 LinearLayout 之上:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/main_layout"
android:gravity="bottom">
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:background="#e6e6e6"
android:layout_height="match_parent">
<ListView
android:id="@+id/navList"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:background="#cccccc"/>
</android.support.v4.widget.DrawerLayout>
我如何设法在 DrawerLayout 之后添加文本,因为现在添加文本会将 DrawerLayout 向下推(如果不清楚,我希望 DrawerLayout 在高度上保持在页面顶部,同时呈现文本“旁边")
最佳答案
DrawerLayout 是这样工作的
To add a navigation drawer, declare your user interface with a DrawerLayout object as the root view of your layout. Inside the DrawerLayout, add one view that contains the main content for the screen (your primary layout when the drawer is hidden) and another view that contains the contents of the navigation drawer.
Check this information regarding DrawerLayouts
你的布局文件应该有 2 个 View :
1 - 主要内容(抽屉关闭时)
2 - 抽屉本身
我希望你的布局是这样的
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view : here you define the layout of your activity -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The main content view : here you define the layout of your activity -->
<LinearLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The navigation drawer -->
<ListView
android:id="@+id/navList"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:background="#cccccc"/>
<LinearLayout/>
</android.support.v4.widget.DrawerLayout>
此外,当您指定:
content.addView(textView, 0);
您严格来说是想将 textView 添加到内容的第一个位置。这会在每个其他之前添加您的 textView,导致 textView 在 drawerLayout 之前 Check this reference regarding ViewGroups and adding views
所以,修改后的代码是
..
private LinearLayout contentFrame;
..
contentFrame = (LinearLayout) findViewById(R.id.content_frame);
...
private void switchTo(String text) {
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0.0F);
final TextView textView = new TextView(this);
textView.setLayoutParams(params);
textView.setText("New text: " + text);
contentFrame.addView(textView);
}
关于android - addView 将文本推送到抽屉布局下方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38785481/
我需要在一个循环中添加很多 View ,而这个 fragment 就是这样做的,应用程序还将有一个抽屉导航和操作栏,用户可以在其中执行操作。 所以我希望这个过程不会 a) 通过阻止用户来减慢应用程序,
以下是我的代码。我正在尝试向我已经创建的 LinearLayout *activity_dynamic* 添加一个 View public void onClick(View v) {
我是 android 应用程序开发的新手,我遇到了一个问题: 当按下我的 DrawerLayout 上的按钮时,我正在向我的主 LinearLayout 添加内容: private void swit
我正在尝试将 TextView 动态添加到线性布局,假设 TextView 是 boo,线性布局是 foo。 TextView boo = new TextView(this) LinearLayou
在 Android 中,当我将像 ImageView 这样的 View 对象添加到 FrameLayout View ,然后立即尝试调用 layout() 方法来重新定位添加的 View 对象时,它不
尝试通过添加新 Activity 和移动到该 Activity 的按钮来编辑 Android 教程应用程序,但在按下“发送”按钮后,我不断收到运行时异常错误(应用程序崩溃)。研究过许多类似的问题,但对
这是 LinearLayout 中的标准 addView。 table.addView(child); 但我需要用这种方式在我的布局中添加 View 请问有可能吗??! 最佳答
我需要用自己的自定义工具栏替换普通的工具栏。我使用以下代码,但没有显示任何内容: 我的MainActivity: public class MainActivity extends AppCo
我尝试在 FrameLayout fragment 中查看相机。但是,我得到了 java.lang.NullPointerException。这是 fragment 代码: public class
我最近开始为 android 开发,但在尝试向布局添加多个 View 时遇到了困难。它实际上适用于第一次触摸,但之后它什么都不做。日志虽然继续工作并显示已检测到触摸并显示事件的位置。我有以下代码 pu
正如您在图片中看到的,我可以选择 View 并使用“_root.addView(dragIcon);”添加 3 个 ImageView。 我的问题是 View 是一个简单 View 而不是 ViewG
我需要有关 ViewFlipper 的简单实现方面的帮助。当我在 Activity 的 onCreate 中膨胀 subview “ws.addView(childView)”时,脚蹼工作得很好。 当
我正在尝试使用 Framework7 作为我的 UI 创建一个 Cordova 应用程序。我正在尝试使用内联页面作为布局架构,但在设置我的项目时,我在控制台中收到以下错误: 未捕获的类型错误:myAp
我想通过 layoutInflater 将一个布局放入另一个布局。 这是 main_layout.xml 文件: 这是 vitrin_layout.xml 文件: 最后是我主
这是我的 MainActivity XML 文件: 这是 .java 文件: @Override protected void onCreate(Bundle savedInstanceSt
当我尝试运行以下代码时出现错误。模拟器给我这个错误信息,然后应用程序强制关闭:应用程序意外停止。 出了什么问题? import edu.chl.dat255.bluebanana.R; import
我对 ViewGroup.addView() 方法有疑问。我使用此代码向我的布局添加新 View : TalbeLayout parent = (TableLayout)findViewById(R.
我想制作一个 ScrollView 中的项目列表。所以我将 LinearLayout 放在 Scrollview 中。linearLayout 中已经有一个项目:添加项目的 View 。我希望该项目始
我在使用 LinearLayout 的 addView 方法时遇到问题。我不知道为什么,但如果我添加三个 View ,则只显示第一个。这是代码: Comment[] comments = posts[
我无法理解 ViewGroup 的 addView(View child, int index) 中 index 的功能。index 有什么用? index 可以是负值吗?它会影响 layout 或显
我是一名优秀的程序员,十分优秀!