一千萬個為什麽

搜索

在運行時更改邊距

我試圖通過在運行時使用更改它們的邊距來更改FrameLayout中某些TextView的位置

MarginLayoutParams newMarginParams = new MarginLayoutParams(view.getLayoutParams());
newMarginParams.setMargins(x, y, 0, 0);
FrameLayout.LayoutParams newLayoutParams = new FrameLayout.LayoutParams(
        newMarginParams);
view.setLayoutParams(newLayoutParams);

我檢查了x和y值是否正確,但未顯示TextView。我試圖為每個TextView調用invalidate()但它不會改變任何東西......

完整的代碼如下所示。每個TextView的邊距都在updateLayouts(...)中更新。

有人可以幫忙嗎?

public class ARActivity extends Activity implements LocationListener, SensorEventListener{

    FrameLayout mainLayout;
    private CBCameraView cv;

    private SensorManager mSensorManager;
    private LocationManager mLocationManager;
    private Location mDeviceLocation;
    private float mDeviceAzimuth = 0;
    private double mDeviceInclination = 0;

    private static final float K = 0.1f;//filtering coefficient
    private static final float ONE_MINUS_K = 1 - K;//filtering coefficient

    private float xAngleWidth = 52.2f;//Samsung Galaxy S: 52.2
    private float yAngleWidth = 39.4f;//Samsung Galaxy S: 39.4

    private final float screenWidth = GlobalVars.screenWidth;
    private final float screenHeight = GlobalVars.screenHeight;

    private final float[] mR = new float[9];

    private final float[] I = new float[9];

    private final float[] orientation = new float[3];

    private final float[] remappedR = new float[9];

    private float[] mags;
    private float[] accels;

    int nARItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.ar_layout);

        mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
        mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
        mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_NORMAL);
        mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 1, this); 

        mDeviceLocation = GlobalVars.location;

        mainLayout = (FrameLayout) findViewById(R.id.ar_layout); 

        cv = new CBCameraView(this);
        cv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        mainLayout.addView(cv); 


       //Add ARViews

        Location location = new Location("me");
        location.setLatitude(41.371974);
        location.setLongitude(2.166978);

        ARView fs = new ARView(this);
        fs.azimuth = 0;
        fs.inclination = 0;
        fs.location = location;
        fs.setText("Bar seco");       

        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);

        fs.setLayoutParams(params);

        mainLayout.addView(fs);
        nARItems = mainLayout.getChildCount();
    }

    public void onLocationChanged(Location location)
    {        
        mDeviceLocation = location;
    }

    public void onProviderDisabled(String provider){}

    public void onProviderEnabled(String provider){}

    public void onStatusChanged(String provider, int status, Bundle extras){}

    public void onAccuracyChanged(Sensor arg0, int arg1){}

    @Override
    public void onSensorChanged(SensorEvent event) {
        switch (event.sensor.getType()) {
        case Sensor.TYPE_MAGNETIC_FIELD:
            mags = event.values.clone();
            break;
        case Sensor.TYPE_ACCELEROMETER:
            accels = event.values.clone();
            break;
        }
        if (mags != null && accels != null) {
            SensorManager.getRotationMatrix(mR, I, accels, mags);
            SensorManager.remapCoordinateSystem(mR, SensorManager.AXIS_X, SensorManager.AXIS_Z, remappedR);
            SensorManager.getOrientation(remappedR, orientation);
            float tmp = (float) Math.toDegrees(orientation[0]);
            if(tmp<0)
                tmp += 360;

            if(Math.abs(mDeviceAzimuth - tmp)<300)
                mDeviceAzimuth = (float) (tmp * K) + (mDeviceAzimuth * ONE_MINUS_K);
            else
                mDeviceAzimuth = tmp;

            tmp = (float) Math.round(Math.toDegrees(orientation[1]));
            mDeviceInclination = (float)(-tmp * K) + (mDeviceInclination * ONE_MINUS_K);

            updateLayouts(mDeviceAzimuth, (float)mDeviceInclination, mDeviceLocation);
        }
    }

    public void updateLayouts(float azimuth, float zAngle, Location location)
    {       
        float leftArm = azimuth -(xAngleWidth/2);
        float rightArm = azimuth +(xAngleWidth/2);
        if(leftArm < 0)
            leftArm = leftArm + 360;
        if(rightArm > 360)
            rightArm = rightArm - 360;

        float upperArm = zAngle + (yAngleWidth/2);
        float lowerArm = zAngle - (yAngleWidth/2);

        for(int i=1; i< 0)
                    view.azimuth = 360+view.azimuth;
            }

            int x = (int)calcXvalue(leftArm, rightArm, view.azimuth);
            int y = (int)calcYvalue(lowerArm, upperArm, view.inclination);

            MarginLayoutParams newMarginParams = new MarginLayoutParams(view.getLayoutParams());
            newMarginParams.setMargins(x, y, 0, 0);
            FrameLayout.LayoutParams newLayoutParams = new FrameLayout.LayoutParams(
                    newMarginParams);
            view.setLayoutParams(newLayoutParams);
        }

    }

    public class ARView extends TextView
    {    
        public float azimuth;
        public float distance;
        public float inclination;
        public Location location;

        public ARView(Context context)
        {        
            super(context);
        }
    }
}

最佳答案

問題可能在於布局是framelayout。我之前在我的代碼中使用過MarginLayoutParams,即使我更改邊距的部分看起來與你的相同,它也能正常工作。唯一的區別是我使用的是LinearLayout。

轉載註明原文: 在運行時更改邊距