【问题标题】:Button layout changes when starting emulator启动模拟器时按钮布局发生变化
【发布时间】:2016-04-11 09:12:03
【问题描述】:

This is what it shows on the emulator

This is how it is on the Layout

每当我启动模拟器时,按钮的大小和位置都会发生变化。有谁知道为什么会发生这种情况?我对此很陌生,所以我很确定我在这里犯了一些错误。希望得到一些帮助,让我知道

这是我的代码:

public class MainActivity extends Activity {
    private static Button button_sbm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        OnClickButtonListener();
    }

        public void OnClickButtonListener() {
            button_sbm = (Button)findViewById(R.id.button2);
            button_sbm.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                    public void onClick(View v) {
                        Uri uri = Uri.parse("http://google.com/");

                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        startActivity(intent);

【问题讨论】:

  • 首先,以这种方式放置按钮确实是个坏主意。尝试使用 3 个水平线性布局并将按钮放在里面。你确定模拟器和带xml的手机预览一样吗?并将长字符串放在两行,在 android:text 中添加一个“\n”
  • 我确定模拟器是一样的。我应该删除相对布局,并将其替换为 3 个水平布局吗?还是只保留相对,然后添加水平?
  • megha jagdale 的回答正是如此
  • 是的,谢谢,你也知道我后来问的问题吗?当我在按钮 2 上进行活动时,它会在我单击按钮 7 时起作用,而不是在 2 上。
  • 要转到单击按钮的活动,您需要放置一个 android:onclick 参数并在 java 中使用它。告诉我如何在单击按钮时启动 Activity?

标签: android eclipse android-layout


【解决方案1】:
I have changed your layout plz refer this

<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:gravity="center"
    android:orientation="vertical"
    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.rodekruis.MainActivity">


    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button9"
        android:layout_alignLeft="@+id/button10"
        android:layout_marginBottom="20dp"
        android:text="Nieuws" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button10"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_alignTop="@+id/button8"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text="  Afspraak   maken" />


        <Button
            android:id="@+id/button8"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text="  Bezoek  tijden" />

        <Button
            android:id="@+id/button9"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:text="Contact" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button3"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text="  Geef je mening!" />


        <Button
            android:id="@+id/button4"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text=" Route begeleiding" />

        <Button
            android:id="@+id/button1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_alignLeft="@+id/button5"
            android:layout_below="@+id/button8"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:text="Specia-listen" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button5"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text="BWC" />


        <Button
            android:id="@+id/button6"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text="Agenda" />


        <Button
            android:id="@+id/button7"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:text="Praktische
 informatie" />
    </LinearLayout>

</LinearLayout>

【讨论】:

  • 非常感谢,非常感谢!布局是固定的,看起来很棒。唯一改变的是我在“Nieuws”按钮上做了一个活动。当我点击它时,我被提到了一个网页。现在这不再起作用了,而按钮的名称从未改变过。
  • 我发现它将按钮 7 视为按钮 2。按钮 7 和按钮 6。当我在按钮 2 上使用活动并使用指向该活动的链接时,我启动了模拟器,当我单击按钮 2 它不起作用,但是当我单击按钮 7 时,它确实起作用。他们换了什么的,你知道为什么吗?
  • 添加按钮属性“android:clickable="true"
  • 感谢您所做的一切。如果我可以问另一个问题。我想在一个按钮上进行另一个活动,但不知道在哪里放置它。我需要在哪里编写活动,我只需要输入公共空白,还是可以在同一个公共空白内完成?
【解决方案2】:

回答关于onclick的问题:

你应该为你的按钮添加一个 onclick 属性:

<Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button9"
        android:layout_alignLeft="@+id/button10"
        android:layout_marginBottom="20dp"
        android:onclick="button2click"
        android:text="Nieuws" />

并在你的 java 中这样做:

public class MainActivity extends Activity {
    private static Button button_sbm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }

    public void button2click (View v)
        {
             Uri uri = Uri.parse("http://google.com/");

             Intent intent = new Intent(Intent.ACTION_VIEW, uri);
             startActivity(intent);
        }
}

【讨论】:

    猜你喜欢
    • 2018-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    相关资源
    最近更新 更多