2

私は別のレイアウトを含むLinearLayoutを持っています。この"included"レイアウトのマージンをプログラム的に変更する方法

<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を呼び出して、マージンを変更しようとしましたが、正しいアプローチであるかどうかはわかりません。どんな助けでも大歓迎です。

おかげ

答えて

1

は、あなたのIDあなたがsetMargins

に値を渡すことができ

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); 

、あなたは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); 
} 

レイアウトパラメータを渡すことができます

0

私はこのコードが参考に考えて...

include.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> 

MainActivity

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); 
} 

    } 
+0

は「を含みます。xml 'はRelativeLayoutです。 RelativeLayoutでLayoutParamsを取得しようとすると、ClassCastExceptionが返されます。そこでLinearLayoutに切り替え、ViewGroupをLinearLayoutにキャストできません。あらゆる種類の奇妙なエラー:/ – user2453055

0

include.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); 
} 
} 
0

あなたは直接<include>タグ内の余白を変更したり、変更するべきではありません。このタグは、追加の親ビューコンテナを追加せずに、現在のビュー階層にレイアウトリソースを追加するだけです。

基本的には、「含まれているレイアウト」のルートビューコンテナに余白を設定します。つまり、あなたは直接文法Proと含まビューの親は、あなたが使用する必要がありますLinear-layout

LinerLayout.LayoutParams params = (LinerLayout.LayoutParams) includedRootView.getLayoutParams(); 
    params.setMargins(); 
    includedViewRoot.setLayoutParams(params);` 
0

あるとしてあなたは

も寸法によってそれを変更することができ、ルートビューの findViewById()を呼び出して、その上にあなたの余白のすべてを設定する必要があります私は自分のコードを持っている一つの小さな違いがあるものの、私は、あまりにも思ったのです
res/values/dimens.xml  
res/values-small/dimens.xml  
res/values-normal/dimens.xml  
res/values-xlarge/dimens.xml 

enter image description here

関連する問題