我有一个水平视图LinearLayout,我希望在其子视图之间有 50 像素的空白空间。经过一些研究,我发现分隔器可以完成这项工作。我不想使用 XML,而是以编程方式创建所有内容。这是我的代码:
LinearLayout parent = new LinearLayout(getContext());
parent.setOrientation(LinearLayout.HORIZONTAL);
ColorDrawable divider = new ColorDrawable(Color.TRANSPARENT);
divider.setBounds(0, 0, 50, 0);
parent.setDividerDrawable(divider);
parent.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
....children are added here to parent...
Run Code Online (Sandbox Code Playgroud)
不幸的是,它不起作用。孩子们之间没有空白。
有谁知道那里出了什么问题吗?
当我试图将另一个视图带到前面时,我在这里得到一个例外.它发生在我的BoardView类的最后一个方法中,它表示if(lives == 0).任何人都可以看到我应该在哪里调用这个来自?的方法?
public class BoardView extends SurfaceView implements SurfaceHolder.Callback{
Context mContext;
// thread initialization
private BoardThread thread;
Thread timer;
// box variables
Bitmap box =
(BitmapFactory.decodeResource
(getResources(), R.drawable.box));
private int box_x = 140;
private int box_y = 378;
private int boxWidth = box.getWidth();
private int boxHeight = box.getHeight();
// storage
private Vector<Blossom> blossomVector = new Vector<Blossom>();
Iterator<Blossom> dataIterator = blossomVector.iterator();
// counters
private int blossomNum = 0;
private String score;
private int currentScore = 0;
private int lives = …Run Code Online (Sandbox Code Playgroud) android android-linearlayout android-edittext android-framelayout
我创建了名为splash.xml的新布局,如下所示,并放入布局文件夹中.

然后我创建一个名为Splash.java的类,如下所示
package com.example.buttonproj;
import android.R;
import android.app.Activity;
import android.os.Bundle;
public class Splash extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash); //ERROR HERE
}
}
Run Code Online (Sandbox Code Playgroud)
有一个错误setContentView(R.layout.splash).你能告诉我出了什么问题吗?
我正在尝试使用一些线性布局构建网格.不幸的是,"网格"内的按钮填满了所有空间,"网格"并不总是相同的大小.
<ScrollView
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/banner"
android:background="#80000000"
android:contentDescription="@string/about"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="15dp"
android:gravity="center_horizontal"
android:text="@string/modi1"
tools:context=".MenuActivity" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="15dp"
android:gravity="center_horizontal"
android:text="@string/modi2"
tools:context=".MenuActivity" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="10dp"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/szen_diagramm" />
<Button
android:layout_marginTop="10dp"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/szen_countdown" />
</LinearLayout>
<Button
android:layout_marginTop="10dp"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/szen_diagramm" />
<Button
android:layout_marginTop="10dp"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" …Run Code Online (Sandbox Code Playgroud) 当我点击一个按钮时,确实在LinearLayout中,我在这一行上出现了这个错误:
LinearLayout ll = (LinearLayout) view;
Run Code Online (Sandbox Code Playgroud)
我的方法看起来像这样:
public void adjDoa(final View view) {
final CharSequence[] items = {"Get on with it!", "Doable!", "Maybe, maybe not.", "Unlikely!", "When pigs fly!"};
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(GetItActivity.this);
dialogBuilder.setTitle("Choose doability!");
dialogBuilder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
LinearLayout ll = (LinearLayout) view;
TextView text = (TextView)ll.findViewById(R.id.tViewChDoa);
text.setText(items[which]);
}
});
dialogBuilder.create().show();
}
Run Code Online (Sandbox Code Playgroud)
这是什么意思?
我有一个简单的线性布局,里面有2个按钮.
我设置了layout_width ="fill_parent"
但我希望按钮占据屏幕尺寸的70%,同时我希望它们居中.
我如何实现这一目标.
这就是它现在的样子.

我想要这样的东西.

谢谢.
编辑: XML代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud) 我有一个相当奇怪的问题,我希望有人能说清楚.我不确定它是否是Android错误.
我在片段中显示以下布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@color/grey"
android:orientation="vertical"
tools:context="com.testapp.test.TestFragment"
>
<ListView
android:id="@+id/data_listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</ListView>
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
即使我将clipToPadding设置为false,我的ListView仍然剪切到我的LinearLayout的填充!
一个解决办法是从LinearLayout中卸下顶部和底部填充,并将其添加到与clipToPadding设置为false上的ListView ListView的,但我宁愿没有这样做,作为一个特殊的情况,我可能要隐藏ListView例如,在保持填充的同时显示它下面的TextView(和屏幕外),并且希望避免在布局中增加额外的复杂性.
更新: 通过一些测试,我发现了一些有趣的东西.在父LinearLayout上设置clipToPadding没有任何效果,因为ListView本身不会在父LinearLayout内滚动,而是ListView中的子项在ListView中滚动!在使用调试工具进行一些调查之后,这似乎很明显.子节点仅限于ListView本身内部的空间,并且因为ListView设置在LinearLayout的填充内,并且不移动,所以子节点在该空间内滚动.
这是我绘制的ASCII图:
--------- <LinearLayout>
| |
| | [ Padding ]
| |
| ----- | <ListView>
| | c | |
| | o | |
| | n | | Content scrolls
| | t | | within ListView
| | e | | ListView …Run Code Online (Sandbox Code Playgroud) android listview fragment android-layout android-linearlayout
我目前有4个按钮,其中一个将使用Intent打开Web浏览器。我想使用webview打开浏览器。我已经看到了此代码,但不确定如何实现。
如果我使用此代码:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new MyWebViewClient());
Run Code Online (Sandbox Code Playgroud)
并更改xml文件以包含WebView,它将其添加到我不需要的当前主屏幕中。我只想在用户单击按钮后使用WebView。我该怎么做呢?
xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/android"
>
<ScrollView
android:id="@+id/settingsScrollView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
</ScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/Info"
android:layout_height="100dp"
android:layout_width="0dp"
android:layout_weight="1"
android:text="@string/Info"
android:background="@drawable/button_colour"
android:textSize="14sp"
android:textStyle="bold"
/>
<Button
android:id="@+id/Search"
android:layout_height="100dp"
android:layout_width="0dp"
android:layout_weight="1"
android:text="@string/Search"
android:background="@drawable/button_colour"
android:textSize="14sp"
android:textStyle="bold"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/Facebook"
android:layout_height="100dp"
android:layout_width="0dp"
android:layout_weight="1"
android:text="@string/Facebook"
android:background="@drawable/button_colour"
android:textSize="14sp"
android:textStyle="bold"
/>
<Button
android:id="@+id/Twitter"
android:layout_height="100dp"
android:layout_width="0dp"
android:layout_weight="1"
android:text="@string/Twitter"
android:background="@drawable/button_colour"
android:textSize="14sp"
android:textStyle="bold"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
MainActivity:
@Override
protected void onCreate(Bundle …Run Code Online (Sandbox Code Playgroud) 我对Android开发相对较新,并且对设计指南和正确的布局技术没有非常深刻的理解.我正在使用需要如下所示的屏幕:

我目前使用的代码是几个线性布局块的混合,我知道这不是实现这种布局的正确方法.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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.culami.WelcomePageActivity"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@string/welcome_msg"
android:layout_gravity="center"
android:textSize="50sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="horizontal"
android:layout_marginTop="15dp" >
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/userIcon1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:src="@drawable/user1"
android:contentDescription="@string/userProfileMSG"
android:layout_gravity="center"
android:onClick="" />
<TextView
android:id="@+id/nameUser1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@string/nameUser1"
android:layout_gravity="center"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/userIcon2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:src="@drawable/user2"
android:contentDescription="@string/userProfileMSG"
android:layout_gravity="center"
android:onClick="" />
<TextView
android:id="@+id/nameUser2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@string/nameUser2"
android:layout_gravity="center"
android:textSize="24sp" /> …Run Code Online (Sandbox Code Playgroud) 我有一个简单LinearLayout的ImageView布局视图,法线视图的权重分别为0.55和0.45。
除非我给weightsum我的礼物,我不能分手LinearLayout。
有我的后LinearLayout裂开,我试图给一个背景色,以我的看法,但它不是反映。
下面是我的代码:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
>
<ImageView
android:layout_weight=".55"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="5dip"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher_background"
android:contentDescription="TODO" />
<View
android:layout_weight=".45dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:visibility="visible"
android:background="@color/colorPrimary"
></View>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我不知道应该进行哪些更改,以便可以正确拆分视图并设置背景颜色。提前致谢!
android android-linearlayout android-background android-layout-weight