本文介绍了RelativeLayout的比重没有应用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下布局的活动:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/testlayoutOverlays"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/testlayoutMain"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/testlayout_bottom"
        android:layout_width="fill_parent"
        android:layout_height="62dp"
        android:background="#122334" >

        <ImageView
            android:id="@+id/testbtnBlock"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_alignParentLeft="true"
            android:layout_gravity="left|center_vertical"
            android:contentDescription="Test1"
            android:padding="@dimen/padding_medium"
            android:src="@drawable/btnblock" />

        <TextView
            android:id="@+id/testtxtZoomPan"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/testbtnX"
            android:layout_toRightOf="@+id/testbtnBlock"
            android:gravity="center_horizontal"
            android:text="@string/txtZoomPan"
            android:textColor="#FFFFFF" />

        <ImageView
            android:id="@+id/testbtnX"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_alignParentRight="true"
            android:layout_gravity="right|center_vertical"
            android:contentDescription="Test2"
            android:padding="@dimen/padding_medium"
            android:src="@drawable/btnx" />
    </RelativeLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/testlayoutPuzzleInfo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:orientation="horizontal" >

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/testlayoutChronoErrors"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <Chronometer
                android:id="@+id/testchronometer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:format="@string/chronometer_initial_format"
                android:gravity="center" />

            <LinearLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/testlayoutErrors"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <ImageView
                    android:layout_width="1px"
                    android:layout_height="20dp" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

</RelativeLayout>

和以下code:

package minmaxdev.android.picrossanywhere;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.support.v4.app.NavUtils;

public class TestActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    // Capture our button from layout
    ImageView button = (ImageView) findViewById(R.id.testbtnBlock);
    // Register the onClick listener with the implementation above
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            RelativeLayout rv = (RelativeLayout) findViewById(R.id.testlayoutOverlays);
            rv.setGravity(Gravity.CENTER);

            Button btnRetry = new Button(TestActivity.this);
            btnRetry.setId(R.id.btnRetry);
            btnRetry.setBackgroundResource(R.drawable.btnselector);
            RelativeLayout.LayoutParams prmBtn = new RelativeLayout.LayoutParams(Util.DPsToPixels(200, getResources()), Util.DPsToPixels(40, getResources()));
            prmBtn.setMargins(0, 120, 0, 0);
            btnRetry.setLayoutParams(prmBtn);
            // btnRetry.setGravity(Gravity.CENTER_HORIZONTAL);
            btnRetry.setText("Retry");
            btnRetry.setOnClickListener(new ImageView.OnClickListener() {
                public void onClick(View v) {
                    Intent intent = getIntent();
                    finish();
                    startActivity(intent);
                }
            });
            rv.addView(btnRetry);
        }

    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_test, menu);
    return true;
}
}

我想知道:为什么我的动态创建按钮(名为 btnRetry )没有出现在屏幕中央,因为我设置父RelativeLayout的重力中心 RV。 setGravity(Gravity.CENTER)

I would like to know :Why is my dynamically created Button (named btnRetry) not appearing in the center of the screen, since I set the parent RelativeLayout gravity to center with rv.setGravity(Gravity.CENTER) ?

非常感谢您的宝贵时间。

Thank you very much for your time

推荐答案

答案很简单。重力作品为内容,layout_gravity对于使用该视图。来源基本上是: http://stackoverflow.com/a/3482757/180538

The answer is easy. Gravity works for the content, layout_gravity for the view that uses that.Source basically: http://stackoverflow.com/a/3482757/180538

尝试使用LayoutParams与 addRule(RelativeLayout.CENTER_IN_PARENT,RelativeLayout.TRUE);

要回答你的重力的理解:你是对的,但我想,在documentation重要的是:

To answer your gravity understanding: You are right but I guess that this information in the documentation is important:

请注意,由于RelativeLayout的考虑每个子相对于彼此的定位是显著,设定重力将影响所有儿童的定位作为父在一个单一的单元。出现这种情况的孩子已经相对定位后。

我不能在此刻测试你的布局,但我的猜测是,如果引力所施加的时间不会产生预期的结果。

I can't test your layout at the moment but my guess is that the time where gravity is applied doesn't create the expected result.

我个人利用重力只有在LinearLayouts和 centerInParent 的RelativeLayouts。

Personally I would use gravity only in LinearLayouts and the centerInParent for RelativeLayouts.

这篇关于RelativeLayout的比重没有应用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 04:00