3

我有一个 LinearLayout,其中包含不同的布局。像这样的东西

<LinearLayout
                    android:id="@+id/linear_layout"
                    android:layout_width="match_parent"
                    android:layout_height="20dp"
                    android:visibility="gone"
                    tools:visibility="visible">

                    <include
                        layout="@layout/new_layout"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_marginBottom="4dp"
                        android:layout_marginLeft="4dp"
                        android:layout_marginStart="4dp"
                        android:layout_weight="1"/>

</LinearLayout>

现在我想做的是,以编程方式更改包含布局(new_layout)的“marginBottom”。我怎样才能做到这一点?

我尝试调用各种 LayoutParams 并尝试更改边距,但不确定这是否是正确的方法。非常感谢任何帮助。

谢谢

4

5 回答 5

1

试试这个.. 使用您的 id 进行线性布局

    LinearLayout ll =(LinearLayout) findViewById(R.id.linear_layout);

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT, 20);

    layoutParams.setMargins(left,top,right,bottom);

您可以在其中传递 setMargins 中的值

如果你想在 dp 中输入值。试试这个

public void setMargin(Context con,ViewGroup.LayoutParams params,int dp) {

    final float scale = con.getResources().getDisplayMetrics().density;
    // convert the DP into pixel
    int pixel =  (int)(dp * scale + 0.5f); 

    ViewGroup.MarginLayoutParams s =(ViewGroup.MarginLayoutParams)params;
    s.setMargins(pixel,pixel,pixel,pixel);

    yourView.setLayoutParams(params);
}

你可以在哪里传递你的布局参数

于 2016-04-28T04:13:39.920 回答
0

包含.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:id="@+id/include_layout">

<ImageView android:layout_width="90dp"
    android:layout_height="90dp"
    android:src="@drawable/cross" />

</LinearLayout>

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">

<include layout="@layout/include"/>

<TextView android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Hello"
    android:padding="10dp" />

</LinearLayout>

MainActivity.class

public class MainActivity extends AppCompatActivity {

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

    LinearLayout included=(LinearLayout)findViewById(R.id.include_layout);
    LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    params.setMargins(Math.round(10), Math.round(10), Math.round(10), Math.round(10));

    //set margin...
    included.setLayoutParams(params);
}
}
于 2016-04-28T04:52:30.700 回答
0

您也可以按尺寸更改它

res/values/dimens.xml    
res/values-small/dimens.xml    
res/values-normal/dimens.xml    
res/values-xlarge/dimens.xml

在此处输入图像描述

于 2016-04-28T09:02:28.230 回答
0

您不应直接更改或修改<include>标签中的边距。此标记只是在当前视图层次结构中添加布局资源,而不添加额外的父视图容器。

所以基本上你想要做的是在“包含的布局”的根视图容器上设置边距。也就是说,您应该直接调用findViewById()根视图并以编程方式在其上设置所有边距,因为包含视图的父级是Linear-layout您应该使用的

   LinerLayout.LayoutParams params = (LinerLayout.LayoutParams) includedRootView.getLayoutParams();
   params.setMargins();
   includedViewRoot.setLayoutParams(params);` 
于 2016-04-28T05:29:20.580 回答
-1

我认为这段代码很有帮助......

包含.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" 
 android:gravity="center">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" >
</ImageView>

</LinearLayout>

activity_main.xml

 <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" >

<include
    android:id="@+id/include_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="4dp"
    android:layout_marginStart="4dp"
     layout="@layout/include" />

 </LinearLayout>

主要活动

package com.example.stackdemo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;


 public class MainActivity extends Activity {

 LinearLayout include_layout;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    include_layout = (LinearLayout) findViewById(R.id.include_layout);

    @SuppressWarnings("deprecation")
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.FILL_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT);

    layoutParams.setMargins(0, 0, 0, 100);
    include_layout.setLayoutParams(layoutParams);
 }

  }
于 2016-04-28T04:36:14.467 回答