重用xml布局但覆盖src和text属性

时间:2015-07-01 17:02:13

标签: android android-layout

我有一个静态布局文件,比如说:

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

    <ImageView
        android:layout_width="56dp"
        android:layout_height="56dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="24dp"
        android:src="@drawable/some_drawable" />

    <TextView
        android:id="@id/placeholder_error_info"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="something"
        />

</LinearLayout>

我希望能够在整个应用程序中多次重复使用此布局文件,但更改文本&amp; src属性根据每个用例。

我不想复制布局文件,并且自定义视图似乎有点过分。框架中是否有解决方案?

2 个答案:

答案 0 :(得分:1)

我会放弃LinearLayout概念 - 您可以轻松使用TextView

将所有全局属性移至样式

<style name="TextWithImage">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:drawablePadding">24dp</item>
</style>

在你的布局中使用这个TextView覆盖文本和drawable

<TextView
    style="@style/TextWithImage"
    android:drawableTop="@drawable/some_drawable"
    android:text="something" />

这比<include> AFAIK没有任何缺点。唯一的问题是你没有完全控制图像大小,但如果你的drawables有56dp(他们应该)你完全没问题。

答案 1 :(得分:0)

我假设您在活动的#import "ServiceConnector.h" @implementation ServiceConnector{ NSMutableData *receivedData; } -(void)getTest{ //Send to server NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"MY_WEBSITE"]]; [request setHTTPMethod:@"GET"]; //initialize an NSURLConnection with the request NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; if(!connection){ NSLog(@"Connection Failed"); } } -(void)postTest:(NSMutableArray *)carSearches{ //build up the request that is to be sent to the server NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"MY_WEBSITE"]]; [request setHTTPMethod:@"POST"]; NSError *writeError = nil; NSData *data = [NSJSONSerialization dataWithJSONObject:carSearches options:NSJSONWritingPrettyPrinted error:&writeError]; NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"JSON Output: %@", jsonString); [request setHTTPBody:data]; //set the data as the post body [request addValue:[NSString stringWithFormat:@"%lu",(unsigned long)data.length] forHTTPHeaderField:@"Content-Length"]; NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; if(!connection){ NSLog(@"Connection Failed"); } } #pragma mark - Data connection delegate - -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ // executed when the connection receives data if(!receivedData){ receivedData = [[NSMutableData alloc]init]; [receivedData appendData:data]; } } -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ //executed when the connection fails NSLog(@"Connection failed with error: %@",error); } -(void)connectionDidFinishLoading:(NSURLConnection *)connection{ NSLog(@"Request Complete,recieved %lu bytes of data",(unsigned long)receivedData.length); NSString *tmp = [NSString stringWithUTF8String:[receivedData bytes]]; NSLog(@"%@",tmp); NSError *error; NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[NSData dataWithBytes:[receivedData bytes] length:[receivedData length]] options:NSJSONReadingAllowFragments error:&error]; [self.delegate requestReturnedData:dictionary]; } 中夸大了这种布局。在布局膨胀之后,您需要在代码中获得对ImageView和TextView的引用,然后您可以在它们上调用方法。

首先,向ImageView添加一个ID:onCreate()

然后,在您的Java代码中:

android:id="@+id/image

您可以将setImageResource和setText的调用替换为您喜欢的任何内容。祝你好运!